HashMap、TreeMap、Hashtable、LinkedHashMap區別
HashMap
允許設置key和value為null,key存放是亂序的,不支持線程的同步,即任一時刻可以有多個線程同時寫HashMap;可能會導致數據的 不一致。如果需要同步,可以用 Collections的synchronizedMap方法使HashMap具有同步的能力,或者使用ConcurrentHashMap,訪問速度 快,因為它根據key的HashCode 值來存儲數據
public static void main(String[] args) { System.out.println("************************* HashMap *******************************"); HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put("X-rapido", "Admin"); hashMap.put("X-rapido", "User"); // 重復的會替換舊數據 hashMap.put("Y-miya", null); hashMap.put("J-lina", null); System.out.println(hashMap.get(null)); hashMap.put(null, "Manager"); hashMap.put(null, "liner"); hashMap.put("劉仁奎", "好帥"); hashMap.put("PrettyBoy", "X-rapido"); System.out.println(hashMap.get(null) + " : " + hashMap.get("劉仁奎") + "\n"); for (String key : hashMap.keySet()) { System.out.println(key + " : " + hashMap.get(key)); } }
結果
************************* HashMap *******************************
null
liner : 好帥
null : liner
J-lina : null
Y-miya : null
X-rapido : User
PrettyBoy : X-rapido
劉仁奎 : 好帥
TreeMap
不允許key為null,但允許value為null,線程非同步,存放是根據key默認是升序排序,也可以指定排序的比較器,當用Iterator 遍歷TreeMap時,得到的記錄是排過序的。
public static void main(String[] args) { System.out.println("************************* TreeMap *******************************"); TreeMap<String, String> treeMap = new TreeMap<String, String>(); treeMap.put("X-rapido", "Admin"); treeMap.put("X-rapido", "User"); // 重復的會替換舊數據 treeMap.put("Y-miya", null); treeMap.put("J-lina", null); System.out.println(treeMap.get(null)); treeMap.put(null, "Manager"); }
結果
************************* TreeMap *******************************
Exception in thread "main" java.lang.NullPointerException
at java.util.TreeMap.getEntry(TreeMap.java:324)
at java.util.TreeMap.get(TreeMap.java:255)
at com.founder.MapSoft.main(MapSoft.java:32)
正確代碼
System.out.println("************************* TreeMap *******************************"); TreeMap<String, String> treeMap = new TreeMap<String, String>(); treeMap.put("X-rapido", "Admin"); treeMap.put("X-rapido", "User"); // 重復的會替換舊數據 treeMap.put("Y-miya", null); treeMap.put("J-lina", null); treeMap.put("劉仁奎", "好帥"); treeMap.put("PrettyBoy", "X-rapido"); for (String key : treeMap.keySet()) { System.out.println(key+" : "+treeMap.get(key)); }
結果
************************* TreeMap *******************************
J-lina : null
PrettyBoy : X-rapido
X-rapido : User
Y-miya : null
劉仁奎 : 好帥
可以使用System.out.println(treeMap.firstEntry().getKey());和System.out.println(treeMap.firstKey());獲取排完序之后的第一個key和value
Hashtable
key和value都不允許為null,線程同步,即任一時刻只有一個線程能寫Hashtable,因此也導致了Hashtale在寫入時會比較慢
LinkedHashMap
保存插入的順序,線程非同步,在用Iterator遍歷LinkedHashMap時,先得到的記錄肯定是先插入的.在遍歷的時候會比HashMap慢。key和value均允許為空,非同步的
不支持線程的同步,即任一時刻可以有多個線程同時寫HashMap;可能會導致數據的不一致。如果需要同步,可以用 Collections的synchronizedMap方法使HashMap具有同步的能力,或者使用ConcurrentHashMap
實現類 |
Map |
|||
繼承類 |
AbstractMap |
Dictionary |
AbstractMap |
HashMap |
名稱 |
HashMap |
HashTable |
TreeMap |
LinkedHashMap |
是否有序排列 |
n(隨機的) |
n |
y (默認升序) |
y(按插入順序排列) |
線程是否同步 |
n |
y |
n |
n |
允許Key為null |
y |
n |
n |
y |
允許Value為null |
y |
n |
y |
y |
性能 |
快(HashMap的遍歷速度和他的容量有關) |
慢 |
塊 |
慢(遍歷速度只和實際數據有關,和容量無關) |
應用范圍 |
插入、刪除和定位元素,HashMap是最好的選擇 |
線程安全 |
取出來的是排序后的鍵值對,插入、刪除需要維護平衡會犧牲一些效率 |
需要輸出的順序和輸入的順序相同 |
Java Map集合利用比較器Comparator根據Key和Value的排序
參考文章:http://blog.csdn.net/xiaokui_wingfly/article/details/42964695