----------- android教育訓練、java教育訓練、java學習型技術部落格、期待與您交流! ------------
Blog6_1 Map集合基本概念
Map集合:存儲鍵值對類型元素的集合,是鍵值類型集合的頂層接口。
Map集合和Collection集合的差別:
(1)Map集合存儲的是成對存在的元素,而Collection集合存儲的是單列元素;
(2)Map集合鍵唯一,值可以重複,當存儲的元素鍵一樣,值進行覆寫并傳回原來的值,而Collection集合中List存儲元素可以重複,Set集合存儲元素無序唯一。
Map集合的常用子類:HashMap、TreeMap、Properties。
Blog6_2 Map集合功能
A:添加功能
V put(K key,V value)----注意和Collection集合中的add區分。
B:删除功能
V remove(Kkey)----删除指定鍵的鍵值對元素。
C:判斷功能
booleancontainsKey(K key)----判斷是否包含指定鍵的鍵值對元素。
booleancontainsValue(V value)----判斷是否包含指定值的鍵值對元素。
D:擷取功能
V get(K key)----擷取指定鍵的值。
Set<K> keySet()----擷取所有鍵的集合
Collection<V>values()----擷取所有值的集合。
Set<Map.Entry<K,V>> entrySet()----傳回此映射中包含的映射關系的Set 視圖。
E:長度功能
int size()----集合中所有鍵值對的對數。
Blog6_3 HashMap集合存儲字元串對象并周遊
Blog6_3_1 HashMap集合存儲字元串對象
Map<String,String> map = new HashMap<String, String>();
//注意:要存儲的鍵和值分别是什麼類型,在泛型中要表示出來,如上。
map.put("周瑜","小喬");
map.put("諸葛亮","黃月英");
map.put("呂布","貂蟬");
map.put("孫策","大喬")
Blog6_3_2周遊字元串HashMap集合中的元素
周遊Map集合主要使用到了Map集合中的:Set<K>keySet()和get(K key)功能,實作代碼如下:
Set<String> set = map.keySet();//擷取Map集合的所有鍵值并存儲//到一個Set集合中。
//使用增強for周遊Map集合:
for (String key : set) {
String value = map.get(key);//使用get方法擷取值
System.out.println(key + "---" + value);
}
Blog6_3_3鍵值對對象找鍵和值方式周遊
此種方式的思路是擷取鍵值對的對象,再根據鍵值對擷取鍵或者值,如下代碼:
Set<Map.Entry<String,String>> set = map.entrySet();
for (Map.Entry<String,String> me : set) {
System.out.println(me.getKey() +"---" +me.getValue());
}
Blog6_4 HashMap集合存儲自定義對象并周遊
Blog6_4_1 HashMap集合存儲自定義對象代碼如下:
public staticvoidmain(String[] args) {
// 建立集合對象
HashMap<String, Student> hm = new HashMap<String,Student>();
// 建立學生對象
Student s1 = new Student("林青霞", 27,"女");
Student s2 = new Student("王祖賢", 57,"女");
Student s3 = new Student("馬德華", 37,"男");
// 添加元素
hm.put("it001",s1);
hm.put("it002",s2);
hm.put("it003",s3);<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
值得注意的一點是,當鍵為Student類時,Student類中必須重寫hashCode()和equals()方法,才能進行鍵唯一的存儲。
Blog6_4_2周遊自定義對象HashMap集合中的元素
// 周遊
Set<String>set = hm.keySet();
for (String key : set) {
Student value = hm.get(key);
// System.out.println(key + "---" + value);
System.out.println(key +"---" +value.getName() + "---"
+value.getAge() + "---" + value.getSex());
}
}
Blog6_5 TreeMap集合存儲字元串對象并周遊
Blog6_5_1 TreeMap集合存儲字元串對象
TreeMap<String, String> tm =new TreeMap<String, String>();
tm.put("yn004","王一勇");
tm.put("yn003","謝世成");
tm.put("yn001","羅志祥");
tm.put("yn002","小布");
Blog6_5_2周遊字元串TreehMap集合中的元素
Set<String> set = tm.keySet();
for (String key : set) {
String value = tm.get(key);
System.out.println(key +"---" + value);
}
注意,上面代碼列印出來的結果是:
yn001---羅志祥
yn002---小布
yn003---謝世成
yn004---王一勇
保證鍵的唯一性,實質是String中重寫了hashCode()和equals()方法
根據鍵進行了排序,實質是String類中實作了Comparable<String>接口。
Blog6_5_2鍵值對對象找鍵和值方式周遊
//擷取鍵值對對象:
Set<Map.Entry<String,String>>set=map.entrySet();
//使用增強for對鍵值對對象集合進行周遊
for(Map.Entry<String,String> me:set){
System.out.println(me.getKey()+"--"+me.getValue());
}
Blog6_6 TreeMap集合存儲鍵自定義對象并周遊
Blog6_6_1TreeMap集合存儲鍵自定義對象并周遊
TreeMap<Student, String> tm = new TreeMap<Student,String>();
// 建立元素對象
Student s1 = new Student("索額圖", 50);
Student s2 = new Student("明珠", 40);
Student s3 = new Student("鳌拜", 47);
Student s4 = new Student("陳廷敬", 43);
Student s5 = new Student("李光地", 44);
//存儲鍵自定義對象
tm.put(s1, "湖北");
tm.put(s2,"河南");
tm.put(s3,"遼甯");
tm.put(s4,"新疆");
tm.put(s5, "山西");
//周遊
Set<Student> set =tm.keySet();
for (Student key : set) {
String value = tm.get(key);
System.out.println(key.getName() +"---"+ key.getAge() +"---"
+value);
}
運作上面程式,發現控制台顯示:treemap2.Student cannot be cast to java.lang.Comparable,是因為TreeMap要進行排序,而鍵是自定義對象,我們并沒有告訴虛拟機按照什麼規則來排序,所有出錯了。解決方法有兩種
A. 讓Student類實作comparable接口
B. 使用TreeMap(Comparator<? superK>comparator)構造方法配合匿名内部類的方法告訴虛拟機怎樣排序。下面我們提供此種方法的代碼:
TreeMap<Student, String> tm = new TreeMap<Student,String>( new Comparator<Student>() {
public int compare(Student s1,Student s2) {
int num1 =s1.getName().length()
-s2.getName().length();
int num2 = (num1 == 0) ?(s1.getAge() - s2.getAge())
:num1;
int num3 = (num2 == 0) ?(s1.getName().compareTo(s2
.getName())): num2;
return num3;
};
});
----------------------- android教育訓練、java教育訓練、java學習型技術部落格、期待與您交流! ----------------------
詳情請檢視:http://edu.csdn.net/heima