How to Convert ArrayList to HashSet in Java? - GeeksforGeeks

15 Nov, 2021
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 :
Using constructor.Using add() method by iterating over each element and adding it into the HashSet.Using addAll() method that adds all the elements in one go into the HashSet.Using stream
Using constructor.
Using add() method by iterating over each element and adding it into the HashSet.
Using addAll() method that adds all the elements in one go into the HashSet.
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.       
Java
// Java program to convert ArrayList// to HashSet using constructor import java.util.HashSet;import java.util.ArrayList;import java.io.*; class GFG {    public static void main(String[] args)    {               // adding elements to ArrayList object        ArrayList<String> gfg = new ArrayList<>();                 gfg.add("data structure");        gfg.add("competitive programming");        gfg.add("Interviews");        gfg.add("FANG");        gfg.add("FANG");         // pass ArrayList object into the HashSet object        // constructor        HashSet<String> hashSet = new HashSet<>(gfg);         // printing every element in Set         for (String value : hashSet) {            System.out.println(value);        }    }}
FANG
data structure
Interviews
competitive programming
Method 2: Using add() method:
In this, we will iterate over Arraylist and add every element in HashSet.
Java
// Java program to convert ArrayList// to HashSet using add() method import java.io.*;import java.util.ArrayList;import java.util.HashSet;class GFG {    public static void main(String[] args)    {        ArrayList<String> gfg = new ArrayList<>();               // adding element to ArrayList        gfg.add("data structure");        gfg.add("competitive programming");        gfg.add("Interviews");        gfg.add("FANG");        gfg.add("FANG");           // adding each Arraylist element inside the HashSet        HashSet<String> hashSet = new HashSet<>();               // printing each element        gfg.forEach(value -> { hashSet.add(value); });                        for (String value : hashSet) {            System.out.println(value);        }    }}
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.
Java
// Java program to convert ArrayList// to HashSet using addAll() method import java.io.*;import java.util.ArrayList;import java.util.HashSet;class GFG {    public static void main(String[] args)    {        ArrayList<String> gfg = new ArrayList<>();         // adding element into ArrayList object        gfg.add("data structure");        gfg.add("competitive programming");        gfg.add("Interviews");        gfg.add("FANG");        gfg.add("FANG");         // adding ArrayList to hashset using addAll() method        HashSet<String> hashSet = new HashSet<>();        hashSet.addAll(gfg);         // printing each element of hashset        for (String value : hashSet) {            System.out.println(value);        }    }}
FANG
data structure
competitve programming
Interviews
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
Java
// Java program to convert ArrayList// to HashSet using streams import java.io.*;import java.util.ArrayList;import java.util.Set;import java.util.HashSet;import java.util.stream.*; class GFG {    public static void main(String[] args)    {        ArrayList<String> gfg = new ArrayList<>();        gfg.add("data structure");        gfg.add("competitive programming");        gfg.add("Interviews");        gfg.add("FANG");        gfg.add("FANG");         // using stream to convert the ArrayList into set        // then typecast into HashSet<String>        HashSet<String> hashSet            = (HashSet<String>)gfg.stream().collect(                Collectors.toSet());         // printing values of the hashSet        for (String value : hashSet) {            System.out.println(value);        }    }}
FANG
data structure
Interviews
competitive programming
akshaysingh98088
Java-ArrayList
java-hashset
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Iterate through List in Java