hashmap键集自动排序

Joc*_*ter 2 java 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"});

    for(String key : content.keySet()){
        System.out.println(key);
    }
Run Code Online (Sandbox Code Playgroud)

上面的代码记录:

View
Help
File
Run Code Online (Sandbox Code Playgroud)

但为什么这样排序?

Bor*_*lov 16

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

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

资源