Java Program to Convert List to HashSet
The List interface provides a way to store the ordered collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements.
The HashSet class permits the null element. The class also offers constant time performance for the basic operations like add, remove, contains, and size assuming the hash function disperses the elements properly among the buckets, which we shall see further in the article.
Note: The set doesn’t allow to store duplicate values. Therefore, any duplicate value in list will be ignored.
The ways to convert List to HashSet :
- Passing List Object as parameter in HashSet.
- Adding each element of List into HashSet using loop.
- Using addAll() Method of Set class.
- Using stream in Java
Method 1: Passing List Object as parameter in HashSet
We use the HashSet constructor for converting it to List.
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<String> L = new ArrayList<String>();
L.add( "Aragorn" );
L.add( "Gandalf" );
L.add( "Legolas" );
L.add( "Frodo" );
HashSet<String> S = new HashSet<String>(L);
System.out.println( "HashSet Elements are : " );
for (String ob : S)
{
System.out.println(ob);
}
}
}
|
Output
HashSet Elements are :
Aragorn
Frodo
Gandalf
Legolas
Method 2: Adding each element of List into HashSet using loop
We simply create a List. We traverse the given List and one by one add elements to the Set.
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> L = new ArrayList<Integer>();
L.add( 1 );
L.add( 4 );
L.add( 30 );
L.add( 100 );
L.add( 15 );
L.add( 30 );
HashSet<Integer> S = new HashSet<Integer>();
for (Integer ob : L)
{
S.add(ob);
}
System.out.println( "HashSet Elements are : " );
for (Object ob : S)
{
System.out.println(ob);
}
}
}
|
Output
HashSet Elements are :
1
4
100
30
15
Method 3: Using addAll() Method of Set class
The java.util.Set.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order.
Syntax:
boolean addAll(Collection C)
Parameters: The parameter C is a collection of any type that is to be added to the set.
Return Value: The method returns true if it successfully appends the elements of the collection C to this Set otherwise it returns False.
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> L = new ArrayList<Integer>();
L.add( 1 );
L.add( 4 );
L.add( 30 );
L.add( 100 );
L.add( 15 );
L.add( 30 );
Set<Integer> S = new HashSet<Integer>();
S.addAll(L);
System.out.println( "HashSet Elements are : " );
for (Object ob : S)
{
System.out.println(ob);
}
}
}
|
Output
HashSet Elements are :
1
4
100
30
15
Method 4: Using stream in Java
Note: Stream only works in Java8 or versions above it.
We use stream in java to convert the given list to stream, then stream to set. This works only in Java 8 or versions after that.
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<String> L = new ArrayList<String>();
L.add( "Rohan" );
L.add( "Ritik" );
L.add( "Yogesh" );
L.add( "Sangeeta" );
L.add( "Palak" );
L.add( "Laxmi" );
Set<String> S = L.stream().collect(Collectors.toSet());
System.out.println( "HashSet Elements are : " );
for (String ob : S)
{
System.out.println(ob);
}
}
}
|
Output:
HashSet Elements are :
1
4
100
30
15
Last Updated :
15 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...