Open In App

How to Convert ArrayList to HashSet in Java?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
1 Like
Like
Report

ArrayList: In Java, ArrayList can have duplicates as well as maintains insertion order.

HashSet: HashSet is the implementation class of Set. It does not allow duplicates and uses Hashtable internally.

There are four ways to convert ArrayList to HashSet :

  1. Using constructor.
  2. Using add() method by iterating over each element and adding it into the HashSet.
  3. Using addAll() method that adds all the elements in one go into the HashSet.
  4. Using stream

Method 1: Using constructor

In this example, we will create an ArrayList object and pass it into the constructor of HashSet. It is the simplest method to convert into HashSet.       


Output
FANG
data structure
Interviews
competitive programming

Method 2: Using add() method:

In this, we will iterate over Arraylist and add every element in HashSet.


Output
FANG
data structure
Interviews
competitive programming

Method 3: Using addAll() method

In this example, we will simply add the complete ArrayList object using addALL() method in the HashSet.


Output
FANG
data structure
Interviews
competitive programming

Method 4: Using Streams.

In this approach we will use streams to iterate over each ArrayList element and then add each item into a set using the collect method. Streams are part of java 8


Output
FANG
data structure
Interviews
competitive programming

Practice Tags :

Similar Reads