18

1:Map(掌握)
(1)將鍵映射到值的對象。一個映射不能包含重複的鍵;每個鍵最多隻能映射到一個值。 
(2)Map和Collection的區別?
A:Map 存儲的是鍵值對形式的元素,鍵唯一,值可以重複。夫妻對
B:Collection 存儲的是單獨出現的元素,子接口Set元素唯一,子接口List元素可重複。光棍
(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:模擬鬥地主洗牌和發牌並對牌進行排序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章