本文介绍了hashmap键集自动排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HashMap< String,String []> content = new HashMap< String,String []>();
content.put(Help,new String [] {About});
content.put(View,new String [] {Grid});
content.put(File,new String [] {Import,Export}); (String key:content.keySet()){
System.out.println(key);
}
以上代码记录:
查看
帮助
文件
为什么这样排序?
解决方案
-
迭代次序。当添加新元素时,它可以(甚至会)完全改变。
- LinkedHashMap将按照条目放入地图的顺序进行迭代 p>
HashMap<String, String[]> content = new HashMap<String, String[]>();
content.put("Help", new String[]{"About"});
content.put("View", new String[]{"Grid"});
content.put("File", new String[]{"Import", "Export"});
for(String key : content.keySet()){
System.out.println(key);
}
The above code logs:
View
Help
File
But why is this sorted?
解决方案
HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.
LinkedHashMap will iterate in the order in which the entries were put into the map
这篇关于hashmap键集自动排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!