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

查看:21
本文介绍了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天全站免登陆