HashMap、LinkedHashMap和treemap的区别.之间的区别是什么?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条经验 获得超3个赞
Map
HashMap
绝对不能保证迭代顺序。当添加新元素时,它甚至可以(而且将)完全改变。 TreeMap
将根据密钥的“自然顺序”按其 compareTo()
方法(或外部提供的 Comparator
)。此外,它还实现了 接口,它包含依赖于此排序顺序的方法。 LinkedHashMap
将按照将条目放入地图的顺序进行迭代。
Hashtable
慕的地6264312
TA贡献1817条经验 获得超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
提交
取消