Add all the elements from a collection to the HashSet in Java



HashSet in Java implements the Set interface, which belongs to the Java Collections Framework and is a part of the java.util package. It stores unique elements, which means we cannot store any duplicate (appearing more than once) values in a HashSet.

The given task is to add all the elements of another collection to a HashSet object in Java. For example, if we have a collection ArrayList with elements [1,3,4,5,5]. Our task is to add all these elements to a HashSet, maybe to remove duplicates from the list, or for any other reason.

Adding Elements from collection to HashSet

In this article, we will learn how to add all the elements from a specified collection to a HashSet in Java using the following ways:

  • Using addAll() method
  • Using a for loop

Using addAll() Method

The addAll() method of the HashSet class (inherited from the Set interface) accepts another collection as a parameter and adds all its elements to the current HashSet object.

Example

In this example, we will create an ArrayList, add some elements to it, and then we will also initialize an empty HashSet. Next, we will add all the elements from the ArrayList to the HashSet and print the values of the HashSet.

import java.util.*;
public class CollectionToHashSet {
   public static void main(String[] args){
      ArrayList list = new ArrayList<>();

      list.add(1);
      list.add(3);
      list.add(2);
      list.add(2);
      list.add(4);

      HashSet<Integer> set = new HashSet<>();
set.addAll(list); System.out.println("HashSet values after adding numbers from a list."); for(int num : set){ System.out.println(num); } } }

The output of the above program will be as follows. Note that duplicates from the list will be added as a single element. For example, in the list, 2 appeared two times, but in the set, it will appear only once.

HashSet values after adding numbers from a list.
1
2
3
4

Using a For Loop

We can also add all the elements from a specified collection to a HashSet using a simple for loop. To do so, we will simply need to traverse through the specified collection and add each element one by one to a HashSet using the add() method.

Example

In this example, we will take a LinkedList collection and add some elements. Then, using a for loop, we will iterate through the LinkedList and add elements to the HashSet.

import java.util.*;
public class CollectionToHashSet {
   public static void main(String[] args){
      LinkedList<Integer> list = new LinkedList<>();
      list.add(1);
      list.add(5);
      list.add(2);
      list.add(6);
      list.add(4);
      list.add(4);

      HashSet<Integer> set = new HashSet<>();
      for(int i = 0; i < list.size(); i++){
         set.add(list.get(i));
      }

      System.out.println("HashSet values after adding numbers from a linked-list.");
      for(int num : set){
         System.out.println(num);
      }
   }
}

Following is the output of the above program. Note that duplicates from the linked list will only appear once in the HashSet.

HashSet values after adding numbers from a linked list.
1
2
4
5
6

Conclusion

In this article, we learned how to add elements from a specified collection to a HashSet. We discussed two approaches: we can simply traverse the collection and add each element one by one to the HashSet, or we can use the addAll() method to add elements in one go.

Aishwarya Naglot
Aishwarya Naglot

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

Updated on: 2025-07-21T15:24:12+05:30

517 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements