HashMap和LinkedHashMap的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HashMap和LinkedHashMap的区别相关的知识,希望对你有一定的参考价值。

一般情况下,我们用的最多的是HashMap,在Map 中插入、删除和定位元素,HashMap 是最好的选择。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。如果需要输出的顺序和输入的相同,那么用LinkedHashMap 可以实现,它还可以按读取顺序来排列.
LinkedHashMap保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的。
Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
		for (BizCode bizCode : allBizCodes) {
			String key = bizCode.getCategory();
			if (!map.containsKey(key)) {
				map.put(key, new LinkedHashMap<String, String>());
			}
			 
		}
在遍历的时候会比HashMap慢TreeMap能够把它保存的记录根据键排序,默认是按升序排序,也可以指定排序的比较器。当用Iterator遍历TreeMap时,得到的记录是排过序的。


本文出自 “JianBo” 博客,请务必保留此出处http://jianboli.blog.51cto.com/12075002/1890668

以上是关于HashMap和LinkedHashMap的区别的主要内容,如果未能解决你的问题,请参考以下文章

LinkedHashMap和HashMap的区别

HashMap与LinkedHashMap的区别

HashMap和LinkedHashMap的区别

HashMap和LinkedHashMap的区别

LinkedHashMap和HashMap的区别以及使用方法

HashMap和LinkedHashMap区别