Java 容器 & 泛型:五、HashMap 和 TreeMap的自白,java容器
Writer:BYSocket(泥沙磚瓦漿木匠)
微博:BYSocket
豆瓣:BYSocket
Java 容器的文章這次應該是最後一篇了:Java 容器 系列。 今天泥瓦匠聊下 Maps。
一、Map回顧
Map,又稱映射表,是將鍵映射到值的對象。有四種實現Map介面並且經常使用的Map集合為:HashMap,TreeMap,Hashtable 和 LinkedHashMap.
泥瓦匠記憶宮殿:
1、一個映射不包含重複的鍵。
2、每個鍵最多隻能映射到一個值。
二、HashMap
HashMap是基於雜湊表的Map介面的實現。其對鍵進行散列,散列函數只能作用於鍵。下面類比下,公司員工和找員工的例子:
?
| 123456789101112131415161718 |
import java.util.HashMap;import java.util.Map; class Employee{} public class HaspMap01{ public static void main(String[] args) { Map<String, Employee> employees = new HashMap<String, Employee>(); employees.put("1206010035", new Employee()); System.out.println(employees); String number = "1206010035"; System.out.println(employees.get(number)); }} |
Run一下,大家可以見到結果:put方法,可以將鍵值映射添加進表。get方法則返回指定鍵所映射的值。從他們 hashCode 可以看出是同一個對象。
HaspMap的鍵必須唯一,同樣其同一個鍵不能存放兩個值,如果對同一個鍵兩次調用put方法,第二個值會取代第一個值。同樣是允許使用 null 值和 null 鍵。下面泥瓦匠用一個簡單的例子解釋下:
?
| 123456789101112131415161718 |
package javaBasic.collection.map; import java.util.HashMap;import java.util.Map; public class HaspMap02{ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void main(String[] args) { Map map = new HashMap<String, String>(); map.put(null, "null01"); map.put(null, "null02"); System.out.println(map); System.out.println(map.get(null)); }} |
結果如下:
?
由此可見,第一個值被第二個值所替換了。
下面有三點是HashMap重要之處:
1、HashMap的建構函式
HaspMap建構函式涉及兩個參數:初始容量和載入因子。初試容量是雜湊表建立時的其中桶的含量。載入因子是雜湊表在其容量自動增加之前可以達到多滿的一種尺度。這兩個參數都是影響HashMap的效能。預設構造一個具有預設初始容量 (16) 和預設載入因子 (0.75)。預設載入因子 (.75) 在時間和空間成本上是一種折衷的考慮。
2、和上次總結的Set都差不多,這個HashMap線程是不安全不同步的。如果想防止意外發生,則設定成同步即可:
?
| 1 |
Map m = Collections.synchronizedMap(new HashMap(...)); |
3、不同步的話,意味著存在快速失敗導致的並發修改異常。
下面看一個複雜例子:
?
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
package javaBasic.collection.map; import java.util.HashMap;import java.util.Map.Entry; class A{ public boolean equals(Object obj) { return true; }} class B{ public int hashCode() { return 1; }} class C{ public int hashCode() { return 2; } public boolean equals(Object obj) { return true; }} public class HashMap03{ public static void main(String[] args) { HashMap<A, Integer> hashMapA = new HashMap<A, Integer>(); hashMapA.put(new A(), 10); hashMapA.put(new A(), 5); System.out.println("HashMapA Elements:"); System.out.print("\t" + hashMapA + "\n"); // loop HashMapA for(Entry<A, Integer> entryA : hashMapA.entrySet()) { System.out.println(entryA.getKey().toString()+"-"+entryA.getValue()); } HashMap<B, Integer> hashMapB = new HashMap<B, Integer>(); hashMapB.put(new B(), 10); hashMapB.put(new B(), 5); System.out.println("HashMapB Elements:"); System.out.print("\t" + hashMapB + "\n"); // loop HashMapB for(Entry<B, Integer> entryB : hashMapB.entrySet()) { System.out.println(entryB.getKey().toString()+"-"+entryB.getValue()); } HashMap<C, Integer> hashMapC = new HashMap<C, Integer>(); hashMapC.put(new C(), 10); hashMapC.put(new C(), 5); System.out.println("HashMapC Elements:"); System.out.print("\t" + hashMapC + "\n"); // loop HashMap for(Entry<C, Integer> entryC : hashMapC.entrySet()) { System.out.println(entryC.getKey().toString()+"-"+entryC.getValue()); } }} |
運行一下,可以看到以下結果:
由此可見,其中和 Java 容器 & 泛型:三、HashSet,TreeSet 和 LinkedHashSet比較 中涉及的知識點一致:
集合判斷兩個元素相等不單單是equals方法,並且必須hashCode()方法返回值也要相等。
三、TreeMap
TreeMap使用樹結構實現(紅/黑樹狀結構),集合中的元素進行排序,但是添加、刪除和包含的演算法複雜度為O(log(n))。其實Map特性基本都是一致的,比如看下面的簡單例子:
?
| 12345678910111213 |
public class TreeMap01{ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] args) { Map map = new TreeMap(); map.put("1", "1"); map.put("4", "4"); map.put("2", "2"); map.put("2", "3"); System.out.println(map); }} |
結果如下:
?
從中我們可以看出
1、TreeMap實現了SortedMap,顧名思義,其表示為有排序的集合。
2、同樣其同一個鍵不能存放兩個值,如果對同一個鍵兩次調用put方法,第二個值會取代第一個值。
四、總結
HashMap與TreeMap 1、HashMap通過hashcode對其內容進行快速尋找,而TreeMap中所有的元素都保持著某種固定的順序,如果你需要得到一個有序的結果你就應該使用TreeMap(HashMap中元素的排列順序是不固定的)。HashMap中元素的排列順序是不固定的)。 2、 HashMap通過hashcode對其內容進行快速尋找,而TreeMap中所有的元素都保持著某種固定的順序,如果你需要得到一個有序的結果你就應該使用TreeMap(HashMap中元素的排列順序是不固定的)。集合架構”提供兩種常規的Map實現:HashMap和TreeMap (TreeMap實現SortedMap介面)。 3、在Map 中插入、刪除和定位元素,HashMap 是最好的選擇。但如果您要按自然順序或自訂順序遍曆鍵,那麼TreeMap會更好。使用HashMap要求添加的鍵類明確定義了hashCode()和 equals()的實現。 這個TreeMap沒有調優選項,因為該樹總處於平衡狀態。
Writer:BYSocket(泥沙磚瓦漿木匠)
微博:BYSocket
豆瓣:BYSocket