Skip to content

LinkedHashSet in Java Tutorial with Examples

Java LinkedHashSet tutorial with examples will help you understand how to use the LinkedHashSet class in an easy way. LinkedHashSet in Java is a Hash table and linked list implementation of the Set interface.

Unlike the HashSet class in Java, the LinkedHashSet class offers a predictable iteration order of the elements because it maintains a doubly-linked list running through all of its elements.

Java LinkedHashSet Tutorial with Examples

The LinkedHashSet class provides constant-time performance for the basic operations like add, remove, and contains and allows null values to be added. However, since it a Set, only one null element can be added to it.

The LinkedHashSet implementation is not synchronized. It means that if the application is multithreaded, we need to synchronize the access. We can get the synchronized set from the LinkedHashSet object using the synchronizedSet method of the Collections class.

We can also pass an existing object of the linked hash set instead of creating a new one.

How to create new objects of LinkedHashSet class?

The LinkedHashSet class in Java provides several constructors using which we can create new objects.

The default LinkedHashSet constructor creates a new and empty linked hash set object.

The new LinkedHashSet object created by the default constructor has an initial capacity of 16 and a load factor of 0.75.

We can also specify the initial capacity and load factor while creating a new LinkedHashSet object using below given overloaded constructors.

if you want to create a new LinkedHashSet object from another set or a collection, you can use below given overloaded constructor.

This constructor creates a new LinkedHashSet object having all elements of the specified collection object. The below given example shows how to create a LinkedHashSet object from the existing HashSet object.

Output

LinkedHashSet Methods

Below given are some of the important LinkedHashSet methods along with the explanations on how to use them in your code.

How to add elements to the LinkedHashSet objects using the add and addAll methods?

The add method of the LinkedHashSet class adds the specified element to the linked hash set object.

The add method returns true if the specified element was not already existed and it is added in the set. If the specified element already existed, it returns false.

Output

The addAll method of the LinkedHashSet class adds all elements of the specified collection object to this set.

It returns true if this set was changed as a result of this method call, false otherwise. The below given example shows how to add all elements of the HashSet object to LinkedHashSet object using the addAll method.

Output

Note: Since the linked hash set is an implementation of the Set interface, duplicate elements from the specified collection will not be added by the addAll method.

How to get the size of the LinkedHashSet using the size method?

The LinkedHashSet size method returns the number of elements stored in the set object.

It returns 0 if the LinkedHashSet is empty.

Output

How to check if the LinkedHashSet is empty using the isEmpty method?

The LinkedHashSet isEmpty method returns true if there are no elements in the set object.

It returns false if there is at least one element in the set object.

Output

How to remove elements from the LinkedHashSet using the remove method?

The LinkedHashSet remove method removes the specified element from the set object.

It returns true if the element is found and removed from the set, false otherwise.

Output

Important Note:

If the LinkedHashSet elements are objects of a custom class (user-defined class), then the class must override the equals and hashCode methods for the remove method to work properly. If the custom class does not override these methods, the remove method will not be able to find the specified element in the set.

How to clear or remove all elements from the LinkedHashSet using the clear method?

The LinkedHashSet clear method removes all elements from the set object.

The LinkedHashSet object will be empty after this method call.

Output

How to remove all LinkedHashSet elements that are also contained in another collection using the removeAll method?

The removeAll method removes all elements from this set that are also contained in the specified collection object.

In other words, all the common elements between this set object and specified collection object are removed from this set.

Output

How to iterate over the elements of the LinkedHashSet object?

There are several ways using which we can iterate through LinkedHashSet elements.

Using enhanced for loop

Output

Using an Iterator

We can get an iterator over the elements of the LinkedHashSet using the iterator method.

Once we get an Iterator object, we can iterate through the LinkedHashSet elements using the hasNext and the next methods as given below.

Output

Using the forEach (Java version 8 and later)

If you are using Java version 8 or later, you can use the forEach method to iterate the LinkedHashSet as well.

Output

How to check if the element exists in the LinkedHashSet using the contains method?

The LinkedHashSet contains method returns true if the specified element exists in the set object.

It returns false if the LinkedHashSet does not contain the specified element.

Output

Important Note:

Just like the remove method, if the LinkedHashSet elements are objects of a custom class (user-defined class), then the class must override the equals and hashCode methods for the contains method to work properly. If the custom class does not override these methods, the contains method will always return false even if the specified element is present in the set.

How to convert LinkedHashSet to an array using the toArray method?

The LinkedHashSet toArray method returns an array containing all elements of this set object.

The array elements are in the same order that is returned from the linked hash set iterator i.e. insertion order.

Output

Important Note:

The toArray method returns the same array filled with the LinkedHashSet elements if the specified array is large enough. If the array is bigger in size than the LinkedHashSet object, the array element immediately after the LinkedHashSet elements is set to null.

If the specified array is smaller than the linked hash set object, a new array is created, filled with the set elements and returned. The array creation is a costly operation in terms of performance, and to avoid this, it is suggested to pass the array of the same length to the toArray method.

How to check if the LinkedHashSet contains all elements of other collection using the containsAll method?

The containsAll method returns true if all the elements of the specified collection are also contained in this set object.

It returns true if this linked hash set object contains all the elements of the specified collection object, false otherwise.

The below given example shows how to check if all ArrayList elements are contained in the LinkedHashSet object using the containsAll method.

Output

How to remove all LinkedHashSet elements that are not present in another collection using the retainAll method?

The retainAll method removes all the elements from this set that are not present in the specified collection.

In other words, it retains only elements that are contained in the specified collection object, the rest of the elements of the LinkedHashSet objects are removed from it.

The below given example shows how to keep only common elements between HashSet and LinkedHashSet using the retainAll method.

Output

How to clone the LinkedHashSet object using the clone method?

The clone method of the LinkedHashSet class creates a shallow copy of this set object.

This method does not create a deep copy of the set object, it means that the LinkedHashSet elements are not cloned, only their references are copied.

Output

How to sort LinkedHashSet elements?

There are a couple of ways using which we can sort LinkedHashSet elements as given below.

1. Sort LinkedHashSet using the TreeSet class

The TreeSet in Java automatically sorts the elements according to their natural order or by the Comparator provided in the TreeSet constructor. We can convert the LinkedHashSet to TreeSet to sort the elements as given below.

Output

Important Note:

If the LinkedHashSet elements are objects of a custom class, then the custom class must either implement the Comparable interface or a custom comparator object must be provided in the TreeSet constructor. If none of these is done, the code will throw java.lang.ClassCastException: class cannot be cast to java.lang.Comparable exception.

2. Using the sort method of the Collections class

To sort the LinkedHashSet using the sort method of the Collections class, we first need to convert the LinkedHashSet object to a List like ArrayList or LinkedList using the constructor. Once we get the List object, we can sort the LinkedHashSet elements as given below.

Output

Important Note:

If the LinkedHashSet elements are objects of a custom class, then the custom class must implement the Comparable interface or a custom comparator must be provided in the overloaded sort method.

Below given are the additional LinkedHashSet examples which cover the topics in more detail.

Java LinkedHashSet Examples

References:
Java 8 LinkedHashSet Documentation

Please let me know if you liked the Java LinkedHashSet tutorial with examples in the comments section below.

About the author

Leave a Reply

Your email address will not be published.