How to Convert ArrayList to HashSet in Java?
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
import java.util.HashSet;
import java.util.ArrayList;
import java.io.*;
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" );
HashSet<String> hashSet = new HashSet<>(gfg);
for (String value : hashSet) {
System.out.println(value);
}
}
}
|
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.
Java
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
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" );
HashSet<String> hashSet = new HashSet<>();
gfg.forEach(value -> { hashSet.add(value); });
for (String value : hashSet) {
System.out.println(value);
}
}
}
|
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.
Java
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
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" );
HashSet<String> hashSet = new HashSet<>();
hashSet.addAll(gfg);
for (String value : hashSet) {
System.out.println(value);
}
}
}
|
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
Java
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" );
HashSet<String> hashSet
= (HashSet<String>)gfg.stream().collect(
Collectors.toSet());
for (String value : hashSet) {
System.out.println(value);
}
}
}
|
Output
FANG
data structure
Interviews
competitive programming
Last Updated :
19 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...