Add Elements to Java LinkedHashSet Example

This example shows how to add elements to LinkedHashSet in Java. This example also shows how to add elements to LinkedHashSet object using the add and addAll methods.

How to add elements to LinkedHashSet in Java?

There are a couple of ways using which we can add elements to LinkedHashSet in Java.

1. Add individual elements using the add method

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

The add method returns true if the element was not present in the set object and it was added. It returns false if the element exists in the LinkedHashSet object and hence not added.

Output

As the LinkedHashSet is an implementation of the Set interface, it does not allow duplicate elements.

2. Add all elements of another collection using the addAll method

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

The addAll method returns true if this set was changed due to this method call, false otherwise.

The below given example shows how to add all elements of ArrayList object to the LinkedHashSet object using the addAll method.

Output

As we can see from the output, the only elements which were not present in the LinkedHashSet were added to it. The element “green” was already present in the LinkedHashSet, so it was not added again from the ArrayList object.

Instead of an ArrayList, you can also add all elements of a LinkedList or TreeSet to the LinkedHashSet using the addAll method.

How to add objects of custom class to LinkedHashSet?

Let’s first see an example of adding a custom class objects to the LinkedHashSet.

Output

As we can see from the output, the duplicate objects were added to the LinkedHashSet object. The reason is, the add method (and the addAll method) of the LinkedHashSet class relies on the equals and hashCode methods of the elements to check if the element already exists in the set or not.

Since the Student class has not overridden these methods, the methods inherited from the Object class were used that compares object references, not the actual object content. That is the reason the add method could not find the duplicate Student object the second time we called it.

To solve the problem of duplicate elements being added to the LinkedHashSet, we need to override the equals and hashCode methods in the Student class as given below.

Tip: While working with any type of Set of custom class objects, always override the equals and hashCode methods in your custom class to avoid duplicate elements being added problem.

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

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

References:
Java 8 LinkedHashSet

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping