Java HashSet Add Elements Example

This example shows how to add elements to a HashSet object in Java. This example also shows how to add elements using the add method and addAll method of the HashSet class.

How to add elements to HashSet in Java using the add method?

The add method of the HashSet class adds the specified element to this HashSet object.

The add method adds the specified element only if it is not already present in the set object. It returns true if the specified element was added to the set and returns false if the element was already present in the set object and was not added.

Output

Note: The HashSet class allows a null element to be added to it. However, since the set is a collection of unique elements, we cannot add more than one null element to the HashSet object.

How to add custom class objects to HashSet using the add method?

The add method uses equals and hashCode methods to check if the element already exists in the HashSet object. If the element is an object of the custom class, the custom class needs to override these methods. Let’s see what happens when we do not do that.

Output

As we can see from the output, duplicate objects were added to the HashSet using the add method. Since our Emp custom class has not overridden the equals and hashCode methods, methods inherited from the Object class were used which compare the references, not the actual object contents.

In order to solve duplicate objects being added to the HashSet problem, we need to override the equals and hashCode methods in our custom class as given below.

Output

As we can see from the output, the add method returned false for the duplicate object this time and the object was not added to the HashSet.

How to add all elements of another collection to HashSet using the addAll method?

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

The addAll method adds all elements from the specified collection object only if they are not already present. It returns true if the set was changed as a result of this method call, false otherwise.

Below given example shows how to add all elements from ArrayList to HashSet using the addAll method.

Output

As we can see from the output, the ArrayList element 3 was not added to the HashSet because it was already present. All other elements were added.

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

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

References:
Java 8 HashSet

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 *