標籤:
1:Map(掌握)
Map介面概述
將鍵映射到值的對象
一個映射不能包含重複的鍵
每個鍵最多隻能映射到一個值
Map介面和Collection介面的不同
Map是雙列的,Collection是單列的
Map的鍵唯一,Collection的子體系Set是唯一的
Map集合的資料結構值針對鍵有效,跟值無關
Collection集合的資料結構是針對元素有效
(3)Map介面功能概述(自己補齊)
A:添加功能
B:刪除功能
C:判斷功能
D:擷取功能
E:長度功能
(4)Map集合的遍曆
A:鍵找值
a:擷取所有鍵的集合
b:遍曆鍵的集合,得到每一個鍵
c:根據鍵到集合中去找值
B:索引值對對象找鍵和值
a:擷取所有的索引值對對象的集合
b:遍曆索引值對對象的集合,擷取每一個索引值對對象
c:根據索引值對對象去擷取鍵和值
代碼體現:
Map<String,String> hm = new HashMap<String,String>();
hm.put("it002","hello");
hm.put("it003","world");
hm.put("it001","java");
//方式1 鍵找值
Set<String> set = hm.keySet();
for(String key : set) {
String value = hm.get(key);
System.out.println(key+"---"+value);
}
//方式2 索引值對對象找鍵和值
Set<Map.Entry<String,String>> set2 = hm.entrySet();
for(Map.Entry<String,String> me : set2) {
String key = me.getKey();
String value = me.getValue();
System.out.println(key+"---"+value);
}
(5)HashMap集合的練習
A:HashMap<String,String>
B:HashMap<Integer,String>
C:HashMap<String,Student>
D:HashMap<Student,String>
(6)TreeMap集合的練習
A:TreeMap<String,String>
B:TreeMap<Student,String>
(7)案例
A:統計一個字串中每個字元出現的次數
B:集合的嵌套遍曆
a:HashMap嵌套HashMap
b:HashMap嵌套ArrayList
c:ArrayList嵌套HashMap
d:多層嵌套
2:Collections(理解)
(1)是針對集合進行操作的工具類
(2)面試題:Collection和Collections的區別
A:Collection 是單列集合的頂層介面,有兩個子介面List和Set
B:Collections 是針對集合進行操作的工具類,可以對集合進行排序和尋找等
(3)常見的幾個小方法:
A:public static <T> void sort(List<T> list)
B:public static <T> int binarySearch(List<?> list,T key)
C:public static <T> T max(Collection<?> coll)
D:public static void reverse(List<?> list)
E:public static void shuffle(List<?> list)
(4)案例
A:ArrayList集合儲存自訂對象的排序
B:類比鬥地主洗牌和發牌
C:類比鬥地主洗牌和發牌並對牌進行排序
重踏學習Java路上_Day18(Map,Collections)