Is the order of the values ​​obtained from the HashMap the order of placement

I am trying to figure out the order in which values ​​in a HashMap can be restored. Here is a 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); } } } 

Exit:

 7 apple lemon orange banana litchi mango papaya 

Values ​​are printed in the order in which they were inserted. Is this true in general? I expected the values ​​to be printed in random order. It uses Java 6.

+41
java hashmap
Jan 27
source share
5 answers

Values ​​are printed in the order in which they were inserted. Is this true in general? I expected the values ​​to be printed in random order.

The HashMap API does not define iteration order.

However, if you look at the implementation of HashMap, you can determine that there is a complex transitional relationship between the iteration order, key hash values, the order in which the keys were inserted, and the size of the hash table. This link gets scrambled if the hash table is resized.

In your case, you use the Integer keys, which means that the key hash values ​​are the key values ​​themselves. In addition, you inserted records in order of priority. This leads (randomly) to an iteration order corresponding to the input order. But if you continue to insert more keys, you will find that the iteration order is “wrapped”. Then, when the table goes through a series of changes, the order will become more and more scrambled.

In short, what you see is an artifact of the hash table implementation, and not something that you can (or should) reasonably use. Not least because it can change from one version of Java to the next.

+58
Jan 27 '10 at 5:48
source share

From the Javadoc: HashMap "class does not give any guarantees regarding the order of the map, in particular, it does not guarantee that the order will remain constant over time.

If you need a sequential order, you can use LinkedHashMap (for insertion / access order) or TreeMap (for comparison). Note that they support the order of keys, not values.

+71
Jan 27 2018-10-10T00:
source share

A LinkedHashMap is what you need. From doco, it differs from HashMap in that it maintains a doubly linked list that goes through all of its entries.

+9
Jan 27 '10 at 5:39
source share

Try LinkedHashMap if order is important ... see in JavaDoc

open class LinkedHashMap extends HashMap

The hash table and linked list are map interface implementations, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly linked list by looking at all of its entries. This linked list defines the iteration of the order, which is usually the order in which the keys were inserted into the card (insertion order). Please note that the insertion order does not change if the key is reinserted into the card. (A key k is reinserted into map m if m.put (k, v) is called when m.containsKey (k) returns true immediately before the call.)

+4
Jan 27 2018-10-10T00:
source share

Associated collection of java.util.concurrent ConcurrentSkipListMap . A skiplist allows you to move records in the order of the keys, as well as view them in random order (but not as fast as the HashMap).

There's a good violinist demo applet .

0
Jan 27 '10 at 5:49 on
source share



All Articles