HashMap、LinkedHashMap和treemap的區(qū)別.之間的區(qū)別是什么?HashMap, LinkedHashMap和TreeMap在爪哇?我看不出有什么不同的輸出,因為這三者都有keySet和values..什么是Hashtable是嗎?Map m1 = new HashMap();m1.put("map", "HashMap");m1.put("schildt", "java2");m1.put("mathew", "Hyden");m1.put("schildt", "java2s");
print(m1.keySet()); print(m1.values()); SortedMap sm = new TreeMap();sm.put("map", "TreeMap");sm.put("schildt", "java2");
sm.put("mathew", "Hyden");sm.put("schildt", "java2s");print(sm.keySet()); print(sm.values());LinkedHashMap lm = new LinkedHashMap();
lm.put("map", "LinkedHashMap");lm.put("schildt", "java2");lm.put("mathew", "Hyden");lm.put("schildt", "java2s");print(lm.keySet());
print(lm.values());
3 回答

蠱毒傳說
TA貢獻1895條經(jīng)驗 獲得超3個贊
Map
HashMap
絕對不能保證迭代順序。當(dāng)添加新元素時,它甚至可以(而且將)完全改變。 TreeMap
將根據(jù)密鑰的“自然順序”按其 compareTo()
方法(或外部提供的 Comparator
)。此外,它還實現(xiàn)了 接口,它包含依賴于此排序順序的方法。 LinkedHashMap
將按照將條目放入地圖的順序進行迭代。
Hashtable

慕的地6264312
TA貢獻1817條經(jīng)驗 獲得超6個贊
╔══════════════╦═════════════════════╦═══════════════════╦════════╗ ║ Property ║ HashMap ║ TreeMap ║ LinkedHashMap ║ ╠══════════════╬═════════════════════╬═══════════════════╬════════╣ ║ Iteration ║ no guarantee order ║ sorted according ║ ║ ║ Order ║ will remain constant║ to the natural ║ insertion-order ║ ║ ║ over time ║ ordering ║ ║ ╠══════════════╬═════════════════════╬═══════════════════╬══════╣ ║ Get/put ║ ║ ║ ║ ║ remove ║ O(1) ║ O(log(n)) ║ O(1) ║ ║ containsKey ║ ║ ║ ║ ╠══════════════╬═════════════════════╬═══════════════════╬════════╣ ║ ║ ║ NavigableMap ║ ║ ║ Interfaces ║ Map ║ Map ║ Map ║ ║ ║ ║ SortedMap ║ ║ ╠══════════════╬═════════════════════╬═══════════════════╬═══════════╣ ║ ║ ║ ║ ║ ║ Null ║ allowed ║ only values ║ allowed ║ ║ values/keys ║ ║ ║ ║ ╠══════════════╬═════════════════════╩═══════════════════╩═════════╣ ║ ║ Fail-fast behavior of an iterator cannot be guaranteed ║ ║ Fail-fast ║ impossible to make any hard guarantees in the presence of ║ ║ behavior ║ unsynchronized concurrent modification ║ ╠══════════════╬═════════════════════╦═══════════════════╦══════════╣ ║ ║ ║ ║ ║ ║Implementation║ buckets ║ Red-Black Tree ║ double-linked ║ ║ ║ ║ ║ buckets ║ ╠══════════════╬═════════════════════╩═══════════════════╩══════════╣ ║ Is ║ ║ ║ synchronized ║ implementation is not synchronized ║ ╚══════════════╩══════════════════════════════════════════════════════╝
添加回答
舉報
0/150
提交
取消