HashMap,LinkedHashMap,TreeMap区别

注:去年项目有用到LinkedHashMap,最开始忘记这个,浪费了点时间,刚好看到阿里开发手册,记录一下区别。

HashMap

最多只允许一条记录的键为Null;允许多条记录的值为 Null

非线程安全

LinkedHashMap

保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的。

TreeMap

存入元素的时候,自动对元素根据键进行升序排序

非线程安全

总结

正常情况下,使用HashMap,有其他需求,再使用LinkedHashMap和TreeMap

集合 Key Value 线程安全
TreeMap 不允许为null 允许为null 非线程安全
HashMap 允许为null 允许为null 非线程安全
LindedHashMap 允许为null 允许为null 非线程安全
HashTable 不允许为null 不允许为null 线程安全(效率低)

猜你喜欢

转载自blog.csdn.net/qq_37436998/article/details/83627814