1. 程式人生 > >java學習筆記(17)

java學習筆記(17)

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 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>

2:Collections(理解)
(1)是針對集合進行操作的工具類
(2)面試題:Collection和Collections的區別
A:Collection 是單列集合的頂層介面,有兩個子介面List和Set
B:Collections 是針對集合進行操作的工具類,可以對集合進行排序和查詢等
(3)常見的幾個小方法:
A:public static void sort(List list)
B:public static int binarySearch(List<?> list,T key)
C:public static T max(Collection<?> coll)
D:public static void reverse(List<?> list)
E:public static void shuffle(List<?> list)