Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java order items in a HashMap or a HashTable?

I was wondering how Java orders items in the Map (HashMap or Hashtable) when they are added. Are the keys ordered by the hashcode, memory reference or by allocation precedence...?

It's because I've noticed same pairs in the Map are not always in the same order

like image 631
Eyad Salah Avatar asked May 12 '10 09:05

Eyad Salah


People also ask

Does HashMap have order Java?

HashMap is unordered per the second line of the documentation: This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. Perhaps you can do as aix suggests and use a LinkedHashMap , or another ordered collection.

Is Hashtable ordered in Java?

Hashtable is a data structure that stores data in key-value format. The stored data is neither in sorted order nor preserves the insertion order.

Does HashMap sort automatically Java?

No, HashMap s don't sort their keys automatically. You want a TreeMap for sorting the keys, or a LinkedHashMap to retain the insertion order.

How are hash tables sorted?

Before moving forward, we need to understand that it is not possible to sort a Hashtable since the data is stored by the hashcode of the key, not by the index . So to sort the data of a Hashtable, we need to have a sortable object like an array or an ArrayList. Sorting has to be done on Key or Value.


2 Answers

java.util.HashMap is unordered; you can't and shouldn't assume anything beyond that.

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

java.util.LinkedHashMap uses insertion-order.

This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

java.util.TreeMap, a SortedMap, uses either natural or custom ordering of the keys.

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

like image 134
polygenelubricants Avatar answered Sep 22 '22 23:09

polygenelubricants


First of all: HashMap specifically doesn't provide a stable and/or defined ordering. So anything you observe is simply an implementation detail and you must not depend on it in any way.

Since it is sometimes useful to know the reason for the seemingly random ordering, here's the basic idea:

A HashMap has number of buckets (implemented as an array) in which to store entries.

When an item is added to the map, it is assigned to a buckets based on a value derived of its hashCode and the bucket size of the HashMap. (Note that it's possible that the bucket is already occupied, which is called a collision. That's handled gracefully and correctly, but I'll ignore that handling for the description because it doesn't change the concept).

The perceived ordering of the entires (such as returned by iterating over the Map) depends on the order of the entries in those buckets.

Whenever the size is rehashed (because the map exceeded its fullness threshold), then the number of buckets changes, which means that the position of each element might change, since the bucket position is derived from the number of buckets as well.

like image 36
Joachim Sauer Avatar answered Sep 21 '22 23:09

Joachim Sauer