Skip to content

How to Preserve Insertion Order in Java HashSet Example

This example shows how to maintain or preserve the insertion order of the elements in Java HashSet. The HashSet class may not maintain the insertion order of the elements.

How to preserve the insertion order of the elements in Java HashSet?

Let’s first see what the HashSet Java document says about the element order.

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

So the document clearly says that the order of the elements returned by the HashSet Iterator may not remain constant over time. Let’s now see an example of that.

Output

As we can see from the output, the elements returned in a different order than the insertion order.

So now back to the original question, how do we maintain the insertion order in the HashSet? The short answer is we cannot. It is by design of the HashSet class. You may get the elements in the same order as they were inserted into HashSet, but it is not guaranteed.

If the requirement is to have a Set functionality of uniqueness but still needs to preserve the insertion order of the elements, the LinkedHashSet class should be used instead of the HashSet class. This is what the LinkedHashSet document says.

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order).

So the LinkedHashSet class is designed to offer the Set functionality that HashSet provides while maintaining the insertion order of the elements because it maintains a doubly-linked list of the entries.

Output

As we can see from the output, the elements returned by the LinkedHashSet iterator are returned in the same order in which they were inserted.

How can I convert the HashSet to a LinkedHashSet?

If you have an existing HashSet object, you can convert it to a LinkedHashSet object using the below given copy constructor.

It creates a LinkedHashSet object containing all the elements of the specified collection object.

Output

As we can see from the output, the elements are still not in the insertion order. That is because, when we create the LinkedHashSet object from the HashSet object, the elements are inserted in the LinkedHashSet in the order returned by the HashSet iterator.

So what is the benefit? Well, the element order we see in the output will remain constant in the LinkedHashSet from now on while the HashSet does not guarantee that. If we create a new LinkedHashSet object instead of converting from the HashSet, this problem does not arise.

This example is a part of the HashSet in Java Tutorial with Examples.

Please let me know your views in the comments section below.

References:
Java 8 HashSet

About the author

Leave a Reply

Your email address will not be published.