Map接口常用的實(shí)現(xiàn)類(lèi)有:
1.HashMap
2.Hashtable
3.TreeMap
4.LinkedHashMap
討論1:底層機(jī)制
HashMap與Hashtable基于數(shù)組實(shí)現(xiàn),TreeMap基于樹(shù)型結(jié)構(gòu),底層存儲(chǔ)結(jié)構(gòu)是典型的鏈表結(jié)構(gòu)。LinkedHashMap繼承自HashMap,所以也是基于數(shù)組實(shí)現(xiàn)的。
討論2:繼承關(guān)系
HashMap與TreeMap繼承自AbstractMap,Hashtable繼承自Dictionary,LinkedHashMap繼承自HashMap。
討論3:同步關(guān)系
Hashtable是同步的,而HashMap與TreeMap以及LinkedHashMap不是同步的,可以使用Collections中提供的方法轉(zhuǎn)換為同步的。
討論4:迭代器
迭代器都是快速失敗的(注:參考本系列第一篇List篇)
討論5:不可修改
通過(guò)使用Collections.unmodifiableMap(Map map)來(lái)轉(zhuǎn)換