java day18 雙列集合Map
阿新 • • 發佈:2018-12-10
18.01_集合框架(Map集合概述和特點)
- A:Map<key, value>介面概述
- 檢視API可以知道:
- 將鍵對映到值的物件
- 一個對映不能包含重複的鍵,鍵是唯一的
- 每個鍵最多隻能對映到一個值
- 檢視API可以知道:
- B:Map介面和Collection介面的不同
- Map是雙列的,Collection是單列的
- Map的鍵唯一,Collection的子體系Set是唯一的
- Map集合的資料結構值針對鍵有效,跟值無關;Collection集合的資料結構是針對元素有效
- 單列集合Set底層依賴雙列集合Map
18.02_集合框架(Map集合的功能概述)
- A:Map集合的功能概述
- a:新增功能
- V put(K key,V value):新增元素。
- 如果鍵是第一次儲存,就直接儲存元素,返回null
- 如果鍵不是第一次存在,就用值把以前的值替換掉,返回以前的值
- V put(K key,V value):新增元素。
- b:刪除功能
- void clear():移除所有的鍵值對元素
- V remove(Object key):根據鍵刪除鍵值對元素,並把值返回
- c:判斷功能
- boolean containsKey(Object key):判斷集合是否包含指定的鍵
- boolean containsValue(Object value):判斷集合是否包含指定的值
- boolean isEmpty():判斷集合是否為空
- d:獲取功能
- Set<Map.Entry<K,V>> entrySet():獲取所有鍵值物件
- V get(Object key):根據鍵獲取值
- Set keySet():獲取集合中所有鍵的集合
- Collection values():獲取集合中所有值的集合
- e:長度功能
- int size():返回集合中的鍵值對的個數
- a:新增功能
18.03 Map集合的遍歷之鍵找值
Map集合中沒有迭代器,但是Set集合和Collection集合有迭代
- A:鍵找值思路:
- 獲取所有鍵的集合
- 遍歷鍵的集合,獲取到每一個鍵
- 根據鍵找值
- B:案例演示
-
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)); }
-
18.04 Map集合的遍歷之鍵值對物件找鍵和值
- A:鍵值對物件找鍵和值思路:
- 獲取所有鍵值對物件的集合
- 遍歷鍵值對物件的集合,獲取到每一個鍵值對物件
- 根據鍵值對物件找鍵和值
- B:案例演示
-
Map集合的遍歷之鍵值對物件找鍵和值
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()); }
-
18.05 HashMap集合鍵是Student值是String的案例
- A:案例演示
- HashMap集合鍵是Student值是String的案例
HashMap<Student, String> hm = new HashMap<>();
hm.put(new Student("張三", 23), "北京");
hm.put(new Student("張三", 23), "上海"); // 需要重寫equal方法,不然會存2遍
hm.put(new Student("李四", 24), "廣州");
hm.put(new Student("王五", 25), "深圳");
System.out.println(hm);
18.06 LinkedHashMap的概述和使用
- A:案例演示
- LinkedHashMap的特點
- 底層是連結串列實現的可以保證怎麼存就怎麼取
- LinkedHashMap的特點
18.07 TreeMap集合鍵是Student值是String的案例
- A:案例演示
- TreeMap集合鍵是Student值是String的案例
方法一:
在Student類中重寫Comparable<Student> 介面compareTo方法
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);
// Student類中
@Override
public int compareTo(Student o) {
int num = this.age - o.age; //以年齡為主要條件
return num == 0 ? this.name.compareTo(o.name) : num;
}
方法二
用比較器,傳比較器
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);
18.08 統計字串中每個字元出現的次數
- 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)); }
-
18.09 集合巢狀之HashMap巢狀HashMap
- A:案例演示
- 集合巢狀之HashMap巢狀HashMap
18.10 HashMap和Hashtable的區別
- A:面試題
- HashMap和Hashtable的區別
- Hashtable是JDK1.0版本出現的,是執行緒安全的,效率低,HashMap是JDK1.2版本出現的,是執行緒不安全的,效率高
- Hashtable不可以儲存null鍵和null值,HashMap可以儲存null鍵和null值
- HashMap和Hashtable的區別
- B:案例演示
- HashMap和Hashtable的區別
18.11 Collections工具類的概述和常見方法講解
- A:Collections類概述
- 針對集合操作 的工具類
- 所有方法都是靜態方法,私有,不允許被繼承
- B:Collections成員方法
-
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) // 亂序
18.12 模擬鬥地主洗牌和發牌
- A:案例演示
-
模擬鬥地主洗牌和發牌,牌沒有排序
//買一副撲克 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);
-
18.13 模擬鬥地主洗牌和發牌並對牌進行排序的原理圖解
- A:畫圖演示
- 畫圖說明排序原理
###18.14_集合框架(模擬鬥地主洗牌和發牌並對牌進行排序的程式碼實現)
- A:案例演示
- 模擬鬥地主洗牌和發牌並對牌進行排序的程式碼實現
-
//買一副牌 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(); }
18.15 泛型固定下邊界
放進去是繼承(?extends E),拿出來是? super E
- ? super E
TreeMap(Comparator< ? super E> comparator) // 構造
public static void main(String[] args) {
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()); // 比較器裡寫的是Student類的比較,但是Student類的子類也可以用
ts2.add(new BaseStudent("張三", 33));
ts2.add(new BaseStudent("李四", 13));
ts2.add(new BaseStudent("王五", 23));
ts2.add(new BaseStudent("趙六", 43));
System.out.println(ts2);
}
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;
}
}
- ? extends E
父類引用指向子類物件
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);