Hashmap是否自动排序? [英] Does Hashmap autosort?

查看:636
本文介绍了Hashmap是否自动排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的哈希图:

HashMap<Long, Day> hashMapTest = new HashMap<Long, Day>();

然后我插入 Date.getTime()像这样的哈希表:

and I insert Date.getTime() into this hashmap like :

Date start = new Date(vonDatum.getTime());

for (int i = 0; i < tagediff; i++)
  {
    Day day= new Day(start);
    this.mitarbeiterTagHashMap.put(start.getTime(), day);
    CalendarUtil.addDaysToDate(start, 1);
  }

奇怪的是,当我使用hashmap调用时,顺序完全是其他且键不适合插入:

The strange thing is when I call the hashmap with, the order is completely an other and the keys doesnt fit to the insertion :

for (Long name : hashMapTest.keySet())
 {
     Window.alert(name + ": " + hashMapTest.get(name));
 }


推荐答案


奇怪的是,当我调用hashmap时,顺序完全是
,而键不适合插入:

The strange thing is when i call the hashmap with, the order is completly an other and the keys doesnt fit to the insertion :

HashMap 不维护插入顺序,但还有一个替代方法,称为 LinkedHashMap 保持插入顺序。或者,如果您希望按自然顺序对键进行排序(使用键compareTo方法),则可以使用 TreeMap

HashMap does NOT maintain the order of insertion but there is an alternative called LinkedHashMap that maintains the insertion order. Or if you want the keys to be sorted in natural order(using keys compareTo method) then you may go for TreeMap.

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

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