hashmap键集自动排序

2024-02-20

    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

但为什么要这样排序呢?


  1. HashMap 绝对不保证迭代顺序。当添加新元素时,它甚至可以(并且将会)完全改变。

  2. LinkedHashMap 将按照条目放入映射的顺序进行迭代

Source http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

hashmap键集自动排序 的相关文章

随机推荐