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
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 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
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
