Add elements to HashSet in Java



The java.util.HashSet class is a part of the Java collection framework. It stores unique elements, which means we cannot store the same element more than once. A HashSet can contain only one null value. If we try to add duplicate elements to a HashSet object, the elements will not be added.

Adding elements to HashSet in Java

The following are the methods used to add elements to an existing HashSet object:

Using add() Method

The add() method of the HashSet class is used for adding a single element at a time. It only adds an element if it doesn't exist in the current HashSet. This method returns a Boolean value, which is TRUE if the element we are adding is unique. Otherwise, it returns false and does not add that element.

Example

In the following example, we will create a HashSet and add elements. In addition, we will try to add some elements twice to check if HashSet inserts those twice or not.

import java.util.HashSet;
public class HashSetDemo {
   public static void main(String[] args) {
      HashSet <String> set = new HashSet<>();

      // Add elements to the set
      set.add("Apple");
      set.add("Banana");
      set.add("Orange");

      // Try to add duplicate values
      set.add("Apple");
      set.add("Banana");

      // Print the set
      System.out.println("HashSet contains: " + set);
    }
}

Following is the output of the above program -

HashSet contains: [Apple, Orange, Banana]

As you can see, we tried to add "Apple" and "Banana" twice, but they were added only once.

Using Collections.addAll() Method

The Collections class contains static methods, which perform various operations on collection objects.

The addAll() method of this class accepts a collection object and elements as parameters and adds the specified elements to the given object. Using this method, we can add multiple values at a time.

For example, if you are given a list of certain fruits and asked to add them to the set, you can use Collections.addAll() to insert all the fruits into a HashSet in a single statement, instead of adding each one individually by using add().

Syntax

The syntax of the addAll() method is as follows:

Collections.addAll(Collection<? super T> c, T... elements)

Example

In the following example, we will create a HashSet and add multiple elements.

import java.util.HashSet;
import java.util.Collections;
public class HashSetDemo {
   public static void main(String[] args) {
      HashSet <String> set = new HashSet<>();

      // Add multiple elements to the set at a time
      Collections.addAll(set, "Apple","Cherry","Banana","Mango","Mango");
        
      // Print the set
      System.out.println("HashSet contains: " + set);
   }
}

Following is the output of the above code:

HashSet contains: [Apple, Cherry, Mango, Banana]

As you can see, we tried to add "Mango" twice, but it got added only once.

Conclusion

In this article, we learned what a HashSet is and how to add elements to a HashSet. We used two methods: add() and Collections.addAll(). The add() method is used for adding single elements to collections, while addAll() is used for adding multiple elements at the same time.

Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-07-21T11:49:27+05:30

388 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements