Turn ArrayList into a Set in Java



In this article, let's learn how to convert an ArrayList into a Set in Java. ArrayList is a collection that allows us to store duplicates, whereas Set is a collection that does not allow duplicates.

So, when we convert an ArrayList to a Set, all the duplicate elements will be removed. If someone tries to add duplicate elements to a Set, it will not throw an error but will simply ignore the duplicate elements. Let's take an example:

Input: [1, 2, 3, 4, 5, 5, 6]
Output: [1, 2, 3, 4, 5, 6]

Ways to Convert an ArrayList to a Set

Following are the different approaches to convert an ArrayList to a Set:

Using the constructor of HashSet

An ArrayList can be converted to a set object using the constructor of the HashSet class (which implements the Set interface).

The resultant set will eliminate any duplicate entries present in the list and will contain only the unique values.

The constructor takes a Collection as an argument, and since ArrayList implements the Collection interface, we can pass it directly.

Example

Following is an example where we convert an ArrayList to a Set using the constructor of Set -

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ArrayListToSet {
   public static void main(String[] args) {
      List<Integer> arrayList = new ArrayList<>();
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(2);
      arrayList.add(5);
      arrayList.add(5); 

      Set<Integer> set = new HashSet<>(arrayList);

      System.out.println("ArrayList: " + arrayList);
      System.out.println("Set: " + set);
   }
}

Output

Following is the output of the above code -

ArrayList: [1, 2, 3, 4, 2, 5, 5]
Set: [1, 2, 3, 4, 5]

Using the addAll() Method

The addAll() method of the Set interface adds all elements of the ArrayList to the Set. This method takes a Collection object as an argument and adds all its elements to the current Set.

Example

Following is an example where we convert an ArrayList to a Set using the addAll() method.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ArrayListToSet {
   public static void main(String[] args) {
      List<Integer> arrayList = new ArrayList<>();
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(2);
      arrayList.add(5);
      arrayList.add(5); 

      Set<Integer> set = new HashSet<>();
      set.addAll(arrayList);

      System.out.println("ArrayList: " + arrayList);
      System.out.println("Set: " + set);
   }
}

Output

Following is the output of the above code:

ArrayList: [1, 2, 3, 4, 2, 5, 5]
Set: [1, 2, 3, 4, 5]

Using Stream API

Now, we will use the Stream API introduced in Java 8 to convert an ArrayList to a Set. The stream() method creates a stream from the ArrayList, and then we can use the collect() method to collect the elements into a Set.

Example

Following is an example where we convert an ArrayList to a Set using the Stream API.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class ArrayListToSet {
   public static void main(String[] args) {
      List<Integer> arrayList = new ArrayList<>();
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(2);
      arrayList.add(5);
      arrayList.add(5); 

      Set<Integer> set = arrayList.stream().collect(Collectors.toSet());

      System.out.println("ArrayList: " + arrayList);
      System.out.println("Set: " + set);
   }
}

Output

Following is the output of the above code:

ArrayList: [1, 2, 3, 4, 2, 5, 5]
Set: [1, 2, 3, 4, 5]

Using a for loop

We can use a simple for loop to iterate through the elements of the ArrayList and add them to a Set.

Example

Following is an example where we convert an ArrayList to a Set using a for loop.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ArrayListToSEt{
   public static void main(String[] args){
      List<Integer> arrayList = new ArrayList<>();
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(2);
      arrayList.add(5);
      arrayList.add(5);
      Set<Integer> set = new HashSet<>();
      for(Integer element : arrayList) {
         set.add(element);
      }
      System.out.println("ArrayList: " + arrayList);
      System.out.println("Set: " + set);
   }
}

Output

Following is the output of the above code:

ArrayList: [1, 2, 3, 4, 2, 5, 5]
Set: [1, 2, 3, 4, 5]
Updated on: 2025-05-30T18:10:34+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements