hashmap键集自动排序 [英] hashmap keyset automatically sorted

查看:214
本文介绍了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);


}

以上代码记录:

 查看
帮助
文件

为什么这样排序?

解决方案


  1. 迭代次序。当添加新元素时,它可以(甚至会)完全改变。

  2. LinkedHashMap将按照条目放入地图的顺序进行迭代
  3. 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?

解决方案

  1. HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.

  2. LinkedHashMap will iterate in the order in which the entries were put into the map

Source

这篇关于hashmap键集自动排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆