Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. Few important features of the Java Set interface are as follows:
- The set interface is an unordered collection of objects in which duplicate values cannot be stored.
- The Java Set does not provide control over the position of insertion or deletion of elements.
- Basically, Set is implemented by HashSet, LinkedHashSet, or TreeSet (sorted representation).
- Set has various methods to add, remove clear, size, etc to enhance the usage of this interface.
In this post different methods to convert a List interface to Set in Java are discussed:
Way 1: Naive Approach: The naive solution is to create an empty set and add every element of the specified list to the newly created set. Below is the implementation of the naive approach:
Java
import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
class GFG {
public static <T> Set<T> convertListToSet(List<T> list)
{
Set<T> set = new HashSet<>();
for (T t : list)
set.add(t);
return set;
}
public static void main(String args[])
{
List<String> list = Arrays.asList("GeeksForGeeks",
"Geeks",
"forGeeks",
"A computer portal",
" for ",
"Geeks");
System.out.println("List: " + list);
Set<String> set = convertListToSet(list);
System.out.println("Set from List: " + set);
}
}
|
Output:
List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks]
Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
Way 2: Using Constructor: HashSet constructor can take another collection object to construct a new set containing the elements of the specified list. Below is the implementation of the above approach:
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static & lt;
T& gt;
Set& lt;
T& gt;
convertListToSet(List& lt; T & gt; list)
{
return new HashSet& lt;
>
(list);
}
public static void main(String args[])
{
List& lt;
String& gt; list = Arrays.asList("GeeksForGeeks",
"Geeks",
"forGeeks",
"A computer portal",
" for ",
"Geeks");
System.out.println(" List : " + list);
Set& lt;
String& gt;
set = convertListToSet(list);
System.out.println(" Set from List
: "
+ set);
}
}
|
Output:
List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks]
Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
Way 3: Using Java 8 Stream API: HashSet constructor can take another collection object to construct a new set containing the elements of the specified list. Below is the implementation of the above approach:
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> Set<T> convertListToSet(List<T> list)
{
return list.stream().collect(Collectors.toSet());
}
public static void main(String args[])
{
List<String> list = Arrays.asList("GeeksForGeeks",
"Geeks",
"forGeeks",
"A computer portal",
" for ",
"Geeks");
System.out.println("List: " + list);
Set<String> set = convertListToSet(list);
System.out.println("Set from List: " + set);
}
}
|
Output:
List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks]
Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
Way 4: Using Guava Sets.newHashSet(): Sets.newHashSet() creates a mutable HashSet instance containing the elements of the specified list. Below is the implementation of the above approach:
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> Set<T> convertListToSet(List<T> list)
{
return return Sets.newHashSet(list);
}
public static void main(String args[])
{
List<String> list = Arrays.asList("GeeksForGeeks",
"Geeks",
"forGeeks",
"A computer portal",
" for ",
"Geeks");
System.out.println("List: " + list);
Set<String> set = convertListToSet(list);
System.out.println("Set from List: " + set);
}
}
|
Output:
List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks]
Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
Way 5: Using addAll() method in Collection package
In this, we can convert the list items to set by using addAll() method. For this, we have to import the package java.util.Collection.
This addAll(Collection c) method is a boolean datatype. It is used to add the collection of one object to the collection of another object
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> Set<T> convertListToSet(List<T> list)
{
HashSet<T> set = new HashSet<>();
set.addAll(list);
return set;
}
public static void main(String args[])
{
List<String> list = Arrays.asList(
"GeeksForGeeks" , "Geeks" , "forGeeks" ,
"A computer portal" , "for" , "Geeks" );
System.out.println( "List: " + list);
Set<String> set = convertListToSet(list);
System.out.println( "Set from List: " + set);
}
}
|
Output:
List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks]
Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Nov, 2022
Like Article
Save Article