【发布时间】:2013-07-28 16:34:04
【问题描述】:
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);
}
以上代码日志:
View
Help
File
但是为什么要这样排序呢?
【问题讨论】:
-
运行两次,三次,更多......你会看到不同的结果。
-
碰巧。添加更多的键,你会看到它没有排序。
-
重启了 10 次,但始终是相同的顺序。
-
您可能会运行非常非常多次的代码,并且顺序将是相同的,因为它取决于两个因素 - KEY 的 hashCode 和后备数组的长度。每当您导致 HashMap 的后备数组增长时,您就会失去订单。请覆盖我的教程Internal life of HashMap
-
@VolodymyrLevytskyi 您的链接已损坏,但我能够在网络档案中找到它。见Internal life of HashMap。太棒了,谢谢!