从HashMap中检索值的顺序是插入顺序 [英] Is the order of values retrieved from a HashMap the insertion order

查看:94
本文介绍了从HashMap中检索值的顺序是插入顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出HashMap中的值被/可以检索的顺序。下面的代码片段相同。

  import java.util.HashMap; 

public class HashMapExample {

public static void main(String [] args){
HashMap< Integer,String> hashmap = new HashMap< Integer,String>();
hashmap.put(1,apple);
hashmap.put(2,lemon);
hashmap.put(3,orange);
hashmap.put(4,banana);
hashmap.put(5,荔枝);
hashmap.put(6,mango);
hashmap.put(7,papaya);

System.out.println(hashmap.size());

for(String key:hashmap.values()){
System.out.println(key);
}
}
}



输出:



  7 
苹果
柠檬
橙色
香蕉
荔枝
mango
papaya

这些值按照插入顺序打印。这是真的吗?我期待值可以以任意顺序打印。这是使用Java 6。

解决方案


这些值按照它们的顺序插入。这是真的吗?


HashMap API不定义迭代顺序。然而,如果你看一下HashMap的实现,你可以推断迭代顺序,键的哈希值,顺序之间有一个复杂的瞬时关系密钥的插入和散列表的大小。如果哈希表调整大小,这个关系就会被混乱。



在你的情况中,你使用的是 Integer 密钥的哈希值是密钥值本身。另外,您按键顺序插入条目。这会(偶然!)导致与插入顺序匹配的迭代顺序。但是如果你不断插入更多的键,你会发现迭代顺序环绕。然后,当表格经过一系列的调整大小时,顺序将逐渐变得越来越混乱。

简而言之,你所看到的是散列表实现的人工制品,而不是你可以(或应该)明智地使用的东西。不仅仅是因为它可以从一个Java版本变成下一个。


I am trying figure out the order in which the values in a HashMap are/can be retrieved. Heres the code snippet for the same.

import java.util.HashMap;

public class HashMapExample {

   public static void main(String[] args) {
       HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
       hashmap.put(1, "apple" );
       hashmap.put(2, "lemon" );
       hashmap.put(3, "orange" );
       hashmap.put(4, "banana" );
       hashmap.put(5, "litchi" );
       hashmap.put(6, "mango" );
       hashmap.put(7, "papaya" );

       System.out.println(hashmap.size());

       for (String key : hashmap.values()) {
           System.out.println(key);
       }
   }
}

output:

7
apple
lemon
orange
banana
litchi
mango
papaya

The values are printed in the order in which they have been inserted. Is this true in general? I was expecting the values to be printed in an arbitrary order. This is using Java 6.

解决方案

The values are printed in the order in which they have been inserted. Is this true in general? I was expecting the values to be printed in random order.

The HashMap API does not define the order of iteration.

However, if you look at the implementation of HashMap, you can deduce that there is a complex transient relationship between the iteration order, the keys' hash values, the order in which the keys were inserted and the size of the hashtable. This relationship gets scrambled if the hashtable resizes itself.

In your case, you are using Integer keys which means that the hash values of the keys are the key values themselves. Also, you inserted the entries in key order. This leads (fortuitously!) to the iteration order matching the insertion order. But if you kept inserting more keys, you would find that the iteration order "wraps around". Then as the table goes through a series of resizes, the order will get progressively more and more scrambled.

In short, what you are seeing is an artefact of the hashtable implementation, and not something that you can (or should) sensibly make use of. Not least because it could change from one Java release to the next.

这篇关于从HashMap中检索值的顺序是插入顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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