Convert List to Set in Java
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 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 is discussed:
- 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 Program to convert// List to Set in Java 8importjava.util.*;importjava.util.stream.*;importjava.util.function.Function;classGFG {// Generic function to convert list to setpublicstatic<T> Set<T> convertListToSet(List<T> list){// create an empty setSet<T> set =newHashSet<>();// Add each element of list into the setfor(T t : list)set.add(t);// return the setreturnset;}publicstaticvoidmain(String args[]){// Create a stream of integersList<String> list = Arrays.asList("GeeksForGeeks","Geeks","forGeeks","A computer portal","for","Geeks");// Print the ListSystem.out.println("List: "+ list);// Convert List to streamSet<String> set = convertListToSet(list);// Print the SetSystem.out.println("Set from List: "+ set);}}chevron_rightfilter_noneOutput:
List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks] Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
- Using Constructor: HashSet constructor can take another collection object to construct a new set containing the elements of the specied list.
Below is the implementation of the above approach:
// Java Program to convert// List to Set in Java 8importjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert list to setpublicstatic<T> Set<T> convertListToSet(List<T> list){// create a set from the ListreturnnewHashSet<>(list);}publicstaticvoidmain(String args[]){// Create a stream of integersList<String> list = Arrays.asList("GeeksForGeeks","Geeks","forGeeks","A computer portal","for","Geeks");// Print the ListSystem.out.println("List: "+ list);// Convert List to streamSet<String> set = convertListToSet(list);// Print the SetSystem.out.println("Set from List: "+ set);}}chevron_rightfilter_noneOutput:
List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks] Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
- Using Java 8 Stream API: HashSet constructor can take another collection object to construct a new set containing the elements of the specied list.
Below is the implementation of the above approach:
// Java Program to convert// List to Set in Java 8importjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert list to setpublicstatic<T> Set<T> convertListToSet(List<T> list){// create a set from the Listreturnlist.stream().collect(Collectors.toSet());}publicstaticvoidmain(String args[]){// Create a stream of integersList<String> list = Arrays.asList("GeeksForGeeks","Geeks","forGeeks","A computer portal","for","Geeks");// Print the ListSystem.out.println("List: "+ list);// Convert List to streamSet<String> set = convertListToSet(list);// Print the SetSystem.out.println("Set from List: "+ set);}}chevron_rightfilter_noneOutput:
List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks] Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
- 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 Program to convert// List to Set in Java 8importjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert list to setpublicstatic<T> Set<T> convertListToSet(List<T> list){// create a set from the ListreturnreturnSets.newHashSet(list);}publicstaticvoidmain(String args[]){// Create a stream of integersList<String> list = Arrays.asList("GeeksForGeeks","Geeks","forGeeks","A computer portal","for","Geeks");// Print the ListSystem.out.println("List: "+ list);// Convert List to streamSet<String> set = convertListToSet(list);// Print the SetSystem.out.println("Set from List: "+ set);}}chevron_rightfilter_noneOutput:
List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks] Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
Recommended Posts:
- Program to convert List of String to List of Integer in Java
- Program to convert List of Integer to List of String in Java
- Convert an Iterator to a List in Java
- Program to Convert Set to List in Java
- Program to Convert List to Map in Java
- Program to Convert a Vector to List in Java
- Convert a String to a List of Characters in Java
- Convert List of Characters to String in Java
- Program to Convert List to Stream in Java
- Program to convert Array to List in Java
- Convert String into comma separated List in Java
- Convert a List of String to a comma separated String in Java
- Convert Stream to Set in Java
- Convert a Set to Stream in Java
- Convert an Iterable to Collection in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



