Map集合
- 概述
- 將鍵映射到值的對象
- 一個映射不能包含重復(fù)的鍵
- 每個鍵最多只能映射到一個值
前面我們一直在學(xué)Collection集合缘揪,那么它和Map集合有什么區(qū)別呢?
- Map集合存儲元素是成對出現(xiàn)的义桂,Map集合的鍵是唯一的找筝,值是可重復(fù)的】兜酰可以把這個理解為:夫妻對
- Collection集合存儲元素是單獨出現(xiàn)的袖裕,Collection的兒子Set是唯一的,List是可重復(fù)的溉瓶〖宾可以把這個理解為:光棍
- 注意:
- Map集合的數(shù)據(jù)結(jié)構(gòu)值針對鍵有效谤民,跟值無關(guān)
- Collection集合的數(shù)據(jù)結(jié)構(gòu)是針對元素有效
下面我們來了解Map集合的功能概述
-
添加功能
- V put(K key,V value):添加元素。
public class MapDemo {
public static void main(String[] args) {
// 創(chuàng)建集合對象
Map<String, String> map = new HashMap<String, String>(); System.out.println("put:" + map.put("馬蓉", "王寶強")); System.out.println("put:" + map.put("馬蓉", "宋喆")); // 輸出集合名稱 System.out.println("map:" + map);
}
}
運行結(jié)果:
put:null
put:王寶強
map:{馬蓉=宋喆}
第一次為什么是null呢疾宏?而第二次返回的是王寶強呢张足?最后奇怪的是馬蓉和宋喆真像現(xiàn)實中一樣。我來給大家說說這是為什么坎藐?
- 因為如果鍵是第一次存儲为牍,就直接存儲元素,返回null
- 如果鍵不是第一次存在岩馍,就用值把以前的值替換掉碉咆,返回以前的值
現(xiàn)在明白了吧。
但是我們平時添加元素不是這樣添加的蛀恩,是下面這樣
// 創(chuàng)建集合對象
Map<String, String> map = new HashMap<String, String>();
// 添加元素
map.put("鄧超", "孫儷");
map.put("黃曉明", "楊穎");
map.put("周杰倫", "昆凌");
map.put("劉愷威", "楊冪");
// 輸出集合名稱
System.out.println("map:" + map);
輸出結(jié)果:map:{鄧超=孫儷, 周杰倫=昆凌, 黃曉明=楊穎, 劉愷威=楊冪}我們繼續(xù)
-
刪除功能 void clear():移除所有的鍵值對元素
V remove(Object key):根據(jù)鍵刪除鍵值對元素疫铜,并把值返回
map.clear();//會移除所有的鍵值對元素
System.out.println("map:" + map);//map:{} //所以不建議使用
//V remove(Object key):根據(jù)鍵刪除鍵值對元素,并把值返回 System.out.println("remove:" + map.remove("黃曉明")); System.out.println("remove:" + map.remove("黃曉波"));
// 輸出集合名稱
System.out.println("map:" + map);
-
判斷功能
- boolean containsKey(Object key):判斷集合是否包含指定的鍵
- boolean containsValue(Object value):判斷集合是否包含指定的值
- boolean isEmpty():判斷集合是否為空
//boolean containsKey(Object key):判斷集合是否包含指定的鍵 System.out.println("containsKey:" + map.containsKey("黃曉明")); System.out.println("containsKey:" + map.containsKey("黃曉波"));
// 輸出集合名稱
System.out.println("map:" + map);
//boolean isEmpty():判斷集合是否為空 System.out.println("isEmpty:"+map.isEmpty());
- 長度功能 int size():返回集合中的鍵值對的對數(shù)
//這個也比較簡單了
System.out.println("size:"+map.size());//size:4
- 獲取功能 V get(Object key):根據(jù)鍵獲取值
- Set keySet():獲取集合中所有鍵的集合
- Collection values():獲取集合中所有值的集合
- Set< Map.Entry< K,V>> entrySet():返回的是鍵值對對象的集合
Map子類
- HashMap
- HashMap類概述鍵是哈希表結(jié)構(gòu)双谆,可以保證鍵的唯一性
- 常用案例HashMap< String,String>
- HashMap< Integer,String>
- HashMap< String,Student>
- HashMap< Student,String>
- 上面的也不是非要是學(xué)生對象壳咕,可以是你需求的對象
LinkedHashMap
- 概述
- Map 接口的哈希表和鏈接列表實現(xiàn),具有可預(yù)知的迭代順序佃乘。
- 由哈希表保證鍵的唯一性囱井,不可重復(fù)
- 由鏈表保證鍵盤的有序(存儲和取出的順序一致)
TreeMap
- 概述
- 鍵是紅黑樹結(jié)構(gòu),可以保證鍵的排序和唯一性
Map集合的知識點和它的子類我上面都將到了趣避,下面我們來做道練習(xí)題吧
- HashMap和Hashtable的區(qū)別庞呕?
public class HashtableDemo {
public static void main(String[] args) {
HashMap<String, String> hm = new HashMap<String, String>(); hm.put("android", "hello");
hm.put(null, "world");
hm.put("java", null);
System.out.println(hm);
Hashtable<String, String> ht = new Hashtable<String, String>(); ht.put("android", "hello");
ht.put(null, "world"); //NullPointerException
ht.put("java", null); // NullPointerException
System.out.println(ht);
}
}
在輸出結(jié)果中:HashMap會打印出{null=world, java=null, Android=hello}而在Hashtable中會報錯所以我們就能得出他們的區(qū)別
- HashMap:線程不安全,效率高程帕。允許null鍵和null值
- Hashtable:線程安全住练,效率低。不允許null鍵和null值
Collections類
我們之前學(xué)習(xí)了Collection愁拭,那么它和Collections有什么區(qū)別呢讲逛。
我們先來了解Collections類,然后在來說他們之間的區(qū)別
- Collections類概述
針對集合進行操作的工具類岭埠,都是靜態(tài)方法盏混。 - Collection和Collections的區(qū)別
Collection:是單列集合的頂層接口,有子接口List和Set惜论。
Collections:是針對集合操作的工具類许赃,有對集合進行排序和二分查找的方法 - Collections成員方法
- public static < T> void sort(List list):排序 默認(rèn)情況下是自然順序。
- public static < T> int binarySearch(List< ?> list,T key):二分查找
- public static < T> T max(Collection< ?> coll):最大值
- public static void reverse(List< ?> list):反轉(zhuǎn)
- public static void shuffle(List< ?> list):隨機置換
public class CollectionsDemo {
public static void main(String[] args) {
// 創(chuàng)建集合對象
List<Integer> list = new ArrayList<Integer>();
// 添加元素
list.add(30); list.add(20); list.add(50); list.add(10); list.add(40);
System.out.println("list:" + list);
// public static <T> void sort(List<T> list):排序 默認(rèn)情況下是自然順序馆类。
Collections.sort(list);
System.out.println("自然順序list:" + list);
// public static <T> int binarySearch(List<?> list,T key):二分查找
System.out.println("二分查找:" + Collections.binarySearch(list, 30));
System.out.println("二分查找:"+ Collections.binarySearch(list, 300));
// public static <T> T max(Collection<?> coll):最大值
System.out.println("max:"+Collections.max(list));
// public static void reverse(List<?> list):反轉(zhuǎn)
Collections.reverse(list);
System.out.println("list:" + list);
//public static void shuffle(List<?> list):隨機置換
Collections.shuffle(list);
System.out.println("list:" + list);
// public static void reverse(List<?> list):反轉(zhuǎn)
Collections.reverse(list);
System.out.println("list:" + list);
}
}
運行結(jié)果:
- list:[30, 20, 50, 10, 40]
- 自然順序list:[10, 20, 30, 40, 50]
- 二分查找:2
- 二分查找:-6
- max:50
- list:[50, 40, 30, 20, 10]
- list:[30, 20, 10, 40, 50]
- list:[50, 40, 10, 20, 30]
- 看到結(jié)果是不是二分查找不存在的時候返回-6為什么呢混聊。
- 因為當(dāng)二分查找不存在的時候,它就會返回最大索引+1再+1.
上面的排序功能乾巧,它在默認(rèn)情況下是自然順序句喜,如果我們要存儲一個自定義對象预愤,那么他就不說自然排序了,就要用到我們上一篇講到的比較器Comparator排序了咳胃,大家要注意一下