package com.heima.map; import java.util.Collection; import java.util.HashMap; import java.util.Map; public class Demo1_Map { public static void main(String[] args) { //demo1(); //demo2(); Map<String, Integer> map = new HashMap<>(); map.put("張三", 23); map.put("李四", 24); map.put("王五", 25); map.put("趙六", 26); Collection<Integer> c = map.values(); System.out.println(c); System.out.println(map.size()); } public static void demo2() { Map<String, Integer> map = new HashMap<>(); map.put("張三", 23); map.put("李四", 24); map.put("王五", 25); map.put("趙六", 26); //Integer value = map.remove("張三"); //根據鍵刪除元素,返回鍵對應的值 //System.out.println(value); System.out.println(map.containsKey("張三")); //判斷是否包含傳入的鍵 System.out.println(map.containsValue(100)); //判斷是否包含傳入的值 System.out.println(map); } public static void demo1() { Map<String, Integer> map = new HashMap<>(); Integer i1 = map.put("張三", 23); Integer i2= map.put("李四", 24); Integer i3 = map.put("王五", 25); Integer i4 = map.put("趙六", 26); Integer i5 = map.put("張三", 26); //相同的鍵不存儲,值覆蓋,把被覆蓋的值返回 System.out.println(map); System.out.println(i1); System.out.println(i2); System.out.println(i3); System.out.println(i4); System.out.println(i5); } }
B:案例演示java
Map集合的遍歷之鍵找值面試
HashMap<String, Integer> hm = new HashMap<>(); hm.put("張三", 23); hm.put("李四", 24); hm.put("王五", 25); hm.put("趙六", 26); /*Set<String> keySet = hm.keySet(); //獲取集合中全部的鍵 Iterator<String> it = keySet.iterator(); //獲取迭代器 while(it.hasNext()) { //判斷單列集合中是否有元素 String key = it.next(); //獲取集合中的每個元素,其實就是雙列集合中的鍵 Integer value = hm.get(key); //根據鍵獲取值 System.out.println(key + "=" + value); //打印鍵值對 }*/ for(String key : hm.keySet()) { //加強for循環迭代雙列集合第一種方式 System.out.println(key + "=" + hm.get(key)); }
package com.heima.map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Demo2_Iterator { /** * 經過查看Map集合的api發現沒有iterator方法,那麼雙列集合如何迭代呢? * 根據鍵獲取值 */ public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("張三", 23); map.put("李四", 24); map.put("王五", 25); map.put("趙六", 26); // Integer i = map.get("張三"); //根據鍵獲取值 // System.out.println(i); //獲取全部的鍵 /*Set<String> keySet = map.keySet(); //獲取全部鍵的集合 Iterator<String> it = keySet.iterator(); //獲取迭代器 while(it.hasNext()) { //判斷集合中是否有元素 String key = it.next(); //獲取每個鍵 Integer value = map.get(key); //根據鍵獲取值 System.out.println(key + "=" + value); }*/ //使用加強for循環遍歷 for(String key : map.keySet()) { //map.keySet()是全部鍵的集合 System.out.println(key + "=" + map.get(key)); } } }
B:案例演示算法
Map集合的遍歷之鍵值對對象找鍵和值api
HashMap<String, Integer> hm = new HashMap<>(); hm.put("張三", 23); hm.put("李四", 24); hm.put("王五", 25); hm.put("趙六", 26); /*Set<Map.Entry<String, Integer>> entrySet = hm.entrySet(); //獲取全部的鍵值對象的集合 Iterator<Entry<String, Integer>> it = entrySet.iterator();//獲取迭代器 while(it.hasNext()) { Entry<String, Integer> en = it.next(); //獲取鍵值對對象 String key = en.getKey(); //根據鍵值對對象獲取鍵 Integer value = en.getValue(); //根據鍵值對對象獲取值 System.out.println(key + "=" + value); }*/ for(Entry<String,Integer> en : hm.entrySet()) { System.out.println(en.getKey() + "=" + en.getValue()); }
C:源碼分析數組
package com.heima.map; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class Demo3_Iterator { /** * Map集合的第二種迭代,根據鍵值對對象,獲取鍵和值 * A:鍵值對對象找鍵和值思路: * 獲取全部鍵值對對象的集合 * 遍歷鍵值對對象的集合,獲取到每個鍵值對對象 * 根據鍵值對對象找鍵和值 */ public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("張三", 23); map.put("李四", 24); map.put("王五", 25); map.put("趙六", 26); //Map.Entry說明Entry是Map的內部接口,將鍵和值封裝成了Entry對象,並存儲在Set集合中 /*Set<Map.Entry<String, Integer>> entrySet = map.entrySet(); //獲取每個對象 Iterator<Map.Entry<String, Integer>> it = entrySet.iterator(); while(it.hasNext()) { //獲取每個Entry對象 Map.Entry<String, Integer> en = it.next(); //父類引用指向子類對象 //Entry<String, Integer> en = it.next(); //直接獲取的是子類對象 String key = en.getKey(); //根據鍵值對對象獲取鍵 Integer value = en.getValue(); //根據鍵值對對象獲取值 System.out.println(key + "=" + value); }*/ for(Entry<String, Integer> en : map.entrySet()) { System.out.println(en.getKey() + "=" + en.getValue()); } } }
package com.heima.map; import java.util.HashMap; import com.heima.bean.Student; public class Demo5_HashMap { /* * * A:案例演示 * HashMap集合鍵是Student值是String的案例 * 鍵是學生對象,表明每個學生 * 值是字符串對象,表明學生歸屬地 */ public static void main(String[] args) { HashMap<Student, String> hm = new HashMap<>(); hm.put(new Student("張三", 23), "北京"); hm.put(new Student("張三", 23), "上海"); hm.put(new Student("李四", 24), "廣州"); hm.put(new Student("王五", 25), "深圳"); System.out.println(hm); } }
package com.heima.map; import java.util.LinkedHashMap; public class Demo6_LinkedHashMap { /** * @param args * LinkedHashMap能夠保證怎麼存就怎麼取 */ public static void main(String[] args) { LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>(); lhm.put("張三", 23); lhm.put("李四", 24); lhm.put("趙六", 26); lhm.put("王五", 25); System.out.println(lhm); } }
package com.heima.map; import java.util.Comparator; import java.util.TreeMap; import com.heima.bean.Student; public class Demo7_TreeMap { /** * * A:案例演示 * TreeMap集合鍵是Student值是String的案例 */ public static void main(String[] args) { //demo1(); TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int num = s1.getName().compareTo(s2.getName()); //按照姓名比較 return num == 0 ? s1.getAge() - s2.getAge() : num; } }); tm.put(new Student("張三", 23), "北京"); tm.put(new Student("李四", 13), "上海"); tm.put(new Student("趙六", 43), "深圳"); tm.put(new Student("王五", 33), "廣州"); System.out.println(tm); } public static void demo1() { TreeMap<Student, String> tm = new TreeMap<>(); tm.put(new Student("張三", 23), "北京"); tm.put(new Student("李四", 13), "上海"); tm.put(new Student("王五", 33), "廣州"); tm.put(new Student("趙六", 43), "深圳"); System.out.println(tm); } }
A:案例演示安全
需求:統計字符串中每一個字符出現的次數 String str = "aaaabbbcccccccccc"; char[] arr = str.toCharArray(); //將字符串轉換成字符數組 HashMap<Character, Integer> hm = new HashMap<>(); //建立雙列集合存儲鍵和值數據結構
for(char c : arr) { //遍歷字符數組 /*if(!hm.containsKey(c)) { //若是不包含這個鍵 hm.put(c, 1); //就將鍵和值爲1添加 }else { //若是包含這個鍵 hm.put(c, hm.get(c) + 1); //就將鍵和值再加1添加進來 } //hm.put(c, !hm.containsKey(c) ? 1 : hm.get(c) + 1); Integer i = !hm.containsKey(c) ? hm.put(c, 1) : hm.put(c, hm.get(c) + 1); } for (Character key : hm.keySet()) { //遍歷雙列集合 System.out.println(key + "=" + hm.get(key)); }
package com.heima.test; import java.util.HashMap; public class Test1 { /** * * A:案例演示 * 需求:統計字符串中每一個字符出現的次數 * * 分析: * 1,定義一個須要被統計字符的字符串 * 2,將字符串轉換爲字符數組 * 3,定義雙列集合,存儲字符串中字符以及字符出現的次數 * 4,遍歷字符數組獲取每個字符,並將字符存儲在雙列集合中 * 5,存儲過程當中要作判斷,若是集合中不包含這個鍵,就將該字符看成鍵,值爲1存儲,若是集合中包含這個鍵,就將值加1存儲 * 6,打印雙列集合獲取字符出現的次數 */ public static void main(String[] args) { //1,定義一個須要被統計字符的字符串 String s = "aaaabbbbbccccccccccccc"; //2,將字符串轉換爲字符數組 char[] arr = s.toCharArray(); //3,定義雙列集合,存儲字符串中字符以及字符出現的次數 HashMap<Character, Integer> hm = new HashMap<>(); //4,遍歷字符數組獲取每個字符,並將字符存儲在雙列集合中 for(char c: arr) { //5,存儲過程當中要作判斷,若是集合中不包含這個鍵,就將該字符看成鍵,值爲1存儲,若是集合中包含這個鍵,就將值加1存儲 /*if(!hm.containsKey(c)) { //若是不包含這個鍵 hm.put(c, 1); }else { hm.put(c, hm.get(c) + 1); }*/ hm.put(c, !hm.containsKey(c) ? 1 : hm.get(c) + 1); } //6,打印雙列集合獲取字符出現的次數 for (Character key : hm.keySet()) { //hm.keySet()表明全部鍵的集合 System.out.println(key + "=" + hm.get(key));//hm.get(key)根據鍵獲取值 } } }
package com.heima.map; import java.util.HashMap; import com.heima.bean.Student; public class Demo8_HashMapHashMap { /** * * A:案例演示 * 集合嵌套之HashMap嵌套HashMap * * 需求: * 雙元課堂有不少基礎班 * 第88期基礎班定義成一個雙列集合,鍵是學生對象,值是學生的歸屬地 * 第99期基礎班定義成一個雙列集合,鍵是學生對象,值是學生的歸屬地 * * 不管88期仍是99期都是班級對象,因此爲了便於統一管理,把這些班級對象添加到雙元課堂集合中 */ public static void main(String[] args) { //定義88期基礎班 HashMap<Student, String> hm88 = new HashMap<>(); hm88.put(new Student("張三", 23), "北京"); hm88.put(new Student("李四", 24), "北京"); hm88.put(new Student("王五", 25), "上海"); hm88.put(new Student("趙六", 26), "廣州"); //定義99期基礎班 HashMap<Student, String> hm99 = new HashMap<>(); hm99.put(new Student("唐僧", 1023), "北京"); hm99.put(new Student("孫悟空",1024), "北京"); hm99.put(new Student("豬八戒",1025), "上海"); hm99.put(new Student("沙和尚",1026), "廣州"); //定義雙元課堂 HashMap<HashMap<Student, String>, String> hm = new HashMap<>(); hm.put(hm88, "第88期基礎班"); hm.put(hm99, "第99期基礎班"); //遍歷雙列集合 for(HashMap<Student, String> h : hm.keySet()) { //hm.keySet()表明的是雙列集合中鍵的集合 String value = hm.get(h); //get(h)根據鍵對象獲取值對象 //遍歷鍵的雙列集合對象 for(Student key : h.keySet()) { //h.keySet()獲取集合總全部的學生鍵對象 String value2 = h.get(key); System.out.println(key + "=" + value2 + "=" + value); } } } }
package com.heima.map; import java.util.HashMap; import java.util.Hashtable; public class Demo9_Hashtable { /** * @param args * 面試題 * HashMap和Hashtable的區別 * 共同點: * 底層都是哈希算法,都是雙列集合 * 區別: * 1,HashMap是線程不安全的,效率高,JDK1.2版本 * Hashtable是線程安全的,效率低,JDK1.0版本的 * 2,HashMap能夠存儲null鍵和null值 * Hashtable不能夠存儲null鍵和null值 */ public static void main(String[] args) { HashMap<String, Integer> hm = new HashMap<>(); hm.put(null, 23); hm.put("李四", null); System.out.println(hm); /*Hashtable<String, Integer> ht = new Hashtable<>(); //ht.put(null, 23); ht.put("張三", null); System.out.println(ht);*/ System.out.println("1111111111111"); } }
public static <T> void sort(List<T> list) public static <T> int binarySearch(List<?> list,T key) public static <T> T max(Collection<?> coll) public static void reverse(List<?> list) public static void shuffle(List<?> list)
package com.heima.collections; import java.util.ArrayList; import java.util.Collections; public class Demo1_Collecitons { /** * Collecitons中的常見方法 * public static <T> void sort(List<T> list) public static <T> int binarySearch(List<?> list,T key) public static <T> T max(Collection<?> coll) public static void reverse(List<?> list) public static void shuffle(List<?> list) */ public static void main(String[] args) { //demo1(); //demo2(); ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("c"); list.add("d"); list.add("g"); list.add("f"); //System.out.println(Collections.max(list)); //根據默認排序結果獲取集合中的最大值 //Collections.reverse(list); //反轉集合 Collections.shuffle(list); //隨機置換,能夠用來洗牌 System.out.println(list); } public static void demo2() { ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("c"); list.add("d"); list.add("f"); list.add("g"); System.out.println(Collections.binarySearch(list, "c")); System.out.println(Collections.binarySearch(list, "b")); } public static void demo1() { ArrayList<String> list = new ArrayList<>(); list.add("c"); list.add("a"); list.add("a"); list.add("b"); list.add("d"); System.out.println(list); Collections.sort(list); //將集合排序 System.out.println(list); } }
A:案例演示框架
模擬鬥地主洗牌和發牌,牌沒有排序ide
//買一副撲克 String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; String[] color = {"方片","梅花","紅桃","黑桃"}; ArrayList<String> poker = new ArrayList<>(); for(String s1 : color) { for(String s2 : num) { poker.add(s1.concat(s2)); } } poker.add("小王"); poker.add("大王"); //洗牌 Collections.shuffle(poker); //發牌 ArrayList<String> gaojin = new ArrayList<>(); ArrayList<String> longwu = new ArrayList<>(); ArrayList<String> me = new ArrayList<>(); ArrayList<String> dipai = new ArrayList<>(); for(int i = 0; i < poker.size(); i++) { if(i >= poker.size() - 3) { dipai.add(poker.get(i)); }else if(i % 3 == 0) { gaojin.add(poker.get(i)); }else if(i % 3 == 1) { longwu.add(poker.get(i)); }else { me.add(poker.get(i)); } } //看牌 System.out.println(gaojin); System.out.println(longwu); System.out.println(me); System.out.println(dipai);
package com.heima.test; import java.util.ArrayList; import java.util.Collections; public class Test2 { /** * * A:案例演示 * 模擬鬥地主洗牌和發牌,牌沒有排序 * * 分析: * 1,買一副撲克,其實就是本身建立一個集合對象,將撲克牌存儲進去 * 2,洗牌 * 3,發牌 * 4,看牌 */ public static void main(String[] args) { //1,買一副撲克,其實就是本身建立一個集合對象,將撲克牌存儲進去 String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; String[] color = {"紅桃","黑桃","方片","梅花"}; ArrayList<String> poker = new ArrayList<>(); //拼接花色和數字 for(String s1 : color) { for(String s2 : num) { poker.add(s1.concat(s2)); //concat鏈接兩個字符串 } } poker.add("小王"); poker.add("大王"); //2,洗牌 Collections.shuffle(poker); //3,發牌 ArrayList<String> gaojin = new ArrayList<>(); ArrayList<String> longwu = new ArrayList<>(); ArrayList<String> me = new ArrayList<>(); ArrayList<String> dipai = new ArrayList<>(); for(int i = 0; i < poker.size(); i++) { if(i >= poker.size() - 3) { dipai.add(poker.get(i)); //將三張底牌存儲在底牌集合中 } else if(i % 3 == 0) { gaojin.add(poker.get(i)); } else if(i % 3 == 1) { longwu.add(poker.get(i)); } else { me.add(poker.get(i)); } } //4,看牌 System.out.println(gaojin); System.out.println(longwu); System.out.println(me); System.out.println(dipai); } }
//買一副牌 String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; String[] color = {"方片","梅花","紅桃","黑桃"}; HashMap<Integer, String> hm = new HashMap<>(); //存儲索引和撲克牌 ArrayList<Integer> list = new ArrayList<>(); //存儲索引 int index = 0; //索引的開始值 for(String s1 : num) { for(String s2 : color) { hm.put(index, s2.concat(s1)); //將索引和撲克牌添加到HashMap中 list.add(index); //將索引添加到ArrayList集合中 index++; } } hm.put(index, "小王"); list.add(index); index++; hm.put(index, "大王"); list.add(index); //洗牌 Collections.shuffle(list); //發牌 TreeSet<Integer> gaojin = new TreeSet<>(); TreeSet<Integer> longwu = new TreeSet<>(); TreeSet<Integer> me = new TreeSet<>(); TreeSet<Integer> dipai = new TreeSet<>(); for(int i = 0; i < list.size(); i++) { if(i >= list.size() - 3) { dipai.add(list.get(i)); //將list集合中的索引添加到TreeSet集合中會自動排序 }else if(i % 3 == 0) { gaojin.add(list.get(i)); }else if(i % 3 == 1) { longwu.add(list.get(i)); }else { me.add(list.get(i)); } } //看牌 lookPoker("高進", gaojin, hm); lookPoker("龍五", longwu, hm); lookPoker("馮佳", me, hm); lookPoker("底牌", dipai, hm); } public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer, String> hm) { System.out.print(name + "的牌是:"); for (Integer index : ts) { System.out.print(hm.get(index) + " "); } System.out.println(); }
package com.heima.test; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.TreeSet; public class Test3 { /** * * A:案例演示 * 模擬鬥地主洗牌和發牌並對牌進行排序的代碼實現 * * 分析: * 1,買一副撲克,其實就是本身建立一個集合對象,將撲克牌存儲進去 * 2,洗牌 * 3,發牌 * 4,看牌 */ public static void main(String[] args) { //1,買一副撲克,其實就是本身建立一個集合對象,將撲克牌存儲進去 String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; String[] color = {"紅桃","黑桃","方片","梅花"}; HashMap<Integer, String> hm = new HashMap<>(); //存儲索引和撲克牌 ArrayList<Integer> list = new ArrayList<>(); //存儲索引 int index = 0; //拼接撲克牌並索引和撲克牌存儲在hm中 for(String s1 : num) { //獲取數字 for(String s2 : color) { //獲取顏色 hm.put(index, s2.concat(s1)); list.add(index); //將索引0到51添加到list集合中 index++; } } //將小王添加到雙列集合中 hm.put(index, "小王"); list.add(index); //將52索引添加到集合中 index++; hm.put(index, "大王"); list.add(index); //將52索引添加到集合中 //2,洗牌 Collections.shuffle(list); //3,發牌 TreeSet<Integer> gaojin = new TreeSet<>(); TreeSet<Integer> longwu = new TreeSet<>(); TreeSet<Integer> me = new TreeSet<>(); TreeSet<Integer> dipai = new TreeSet<>(); for(int i = 0; i < list.size(); i++) { if(i >= list.size() - 3) { dipai.add(list.get(i)); //將三張底牌存儲在底牌集合中 }else if(i % 3 == 0) { gaojin.add(list.get(i)); }else if(i % 3 == 1) { longwu.add(list.get(i)); }else { me.add(list.get(i)); } } //看牌 lookPoker(hm, gaojin, "高進"); lookPoker(hm, longwu, "龍五"); lookPoker(hm, me, "馮佳"); lookPoker(hm, dipai, "底牌"); } /* * 看牌 * 1,返回值類型void * 2,參數列表HashMap,TreeSet,String name */ public static void lookPoker(HashMap<Integer, String> hm,TreeSet<Integer> ts ,String name) { System.out.print(name + "的牌是:"); for(Integer i : ts) { //i表明雙列集合中的每個鍵 System.out.print(hm.get(i) + " "); } System.out.println(); } }
package com.heima.collections; import java.util.ArrayList; import java.util.Comparator; import java.util.TreeSet; import com.heima.bean.BaseStudent; import com.heima.bean.Student; public class Demo2_Genric { /** * 泛型固定下邊界 * ? super E * * 泛型固定上邊界 * ? extends E */ public static void main(String[] args) { //demo1(); TreeSet<Student> ts1 = new TreeSet<>(new CompareByAge()); ts1.add(new Student("張三", 33)); ts1.add(new Student("李四", 13)); ts1.add(new Student("王五", 23)); ts1.add(new Student("趙六", 43)); TreeSet<BaseStudent> ts2 = new TreeSet<>(new CompareByAge()); ts2.add(new BaseStudent("張三", 33)); ts2.add(new BaseStudent("李四", 13)); ts2.add(new BaseStudent("王五", 23)); ts2.add(new BaseStudent("趙六", 43)); System.out.println(ts2); } public static void demo1() { ArrayList<Student> list1 = new ArrayList<>(); list1.add(new Student("張三", 23)); list1.add(new Student("李四", 24)); ArrayList<BaseStudent> list2 = new ArrayList<>(); list2.add(new BaseStudent("王五", 25)); list2.add(new BaseStudent("趙六", 26)); list1.addAll(list2); } } class CompareByAge implements Comparator<Student> { @Override public int compare(Student s1, Student s2) { int num = s1.getAge() - s2.getAge(); return num == 0 ? s1.getName().compareTo(s2.getName()) : num; } }