A List in Java is an ordered collection that can contain duplicate elements, whereas a Set is a collection that stores only unique elements. Therefore, converting a List to a Set is useful when duplicate values need to be removed. Java provides several ways to perform this conversion, including using the HashSet constructor, addAll(), and the Stream API.
- HashSet does not maintain the insertion order of elements.
- LinkedHashSet can be used when the original insertion order needs to be preserved.
- TreeSet can be used when the elements need to be stored in sorted order.
Ways to Convert List to Set in Java
1. Using a for Loop
The simplest approach is to create an empty HashSet and add each element of the List to it. Since a Set does not allow duplicates, repeated elements are automatically ignored.
Approach
- Create a List containing the elements.
- Create an empty HashSet.
- Traverse the List using a for loop.
- Add each element to the Set.
- Display the resulting Set.
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static <T> Set<T> convertListToSet(List<T> list) {
Set<T> set = new HashSet<>();
for (T element : list) {
set.add(element);
}
return set;
}
public static void main(String[] args) {
List<String> list = Arrays.asList(
"Java",
"Python",
"Java",
"C++",
"Python"
);
System.out.println("List: " + list);
Set<String> set = convertListToSet(list);
System.out.println("Set from List: " + set);
}
}
Output
List: [Java, Python, Java, C++, Python] Set from List: [Java, C++, Python]
Note: The order of elements in the Set may vary because HashSet does not guarantee insertion order.
2. Using the HashSet Constructor
The HashSet class provides a constructor that accepts a Collection. Since List extends Collection, we can directly pass a List to the HashSet constructor.
Syntax
Set<T> set = new HashSet<>(list);
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static <T> Set<T> convertListToSet(List<T> list) {
return new HashSet<>(list);
}
public static void main(String[] args) {
List<String> list = Arrays.asList(
"Java",
"Python",
"Java",
"C++",
"Python"
);
System.out.println("List: " + list);
Set<String> set = convertListToSet(list);
System.out.println("Set from List: " + set);
}
}
Output
List: [Java, Python, Java, C++, Python] Set from List: [Java, C++, Python]
Explanation: The HashSet constructor receives the List and adds its elements to the Set. Duplicate values such as "Java" and "Python" are automatically removed.
3. Using Java Stream API
Java 8 introduced the Stream API, which provides another convenient way to convert a List into a Set. The Collectors.toSet() method collects the elements of a Stream into a Set.
Syntax
Set<T> set = list.stream().collect(Collectors.toSet());
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class Main {
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(
"Java",
"Python",
"Java",
"C++",
"Python"
);
System.out.println("List: " + list);
Set<String> set = convertListToSet(list);
System.out.println("Set from List: " + set);
}
}
Output
List: [Java, Python, Java, C++, Python] Set from List: [Java, C++, Python]
Explanation: The stream() method creates a Stream from the List. The Collectors.toSet() collector then gathers the Stream elements into a Set, removing duplicate values in the process.
4. Using addAll() Method
The addAll() method is defined in the Collection interface. It adds all elements from one collection to another collection. We can create an empty HashSet and use addAll() to add all elements of the List.
Syntax
set.addAll(list);
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static <T> Set<T> convertListToSet(List<T> list) {
Set<T> set = new HashSet<>();
set.addAll(list);
return set;
}
public static void main(String[] args) {
List<String> list = Arrays.asList(
"Java",
"Python",
"Java",
"C++",
"Python"
);
System.out.println("List: " + list);
Set<String> set = convertListToSet(list);
System.out.println("Set from List: " + set);
}
}
Output
List: [Java, Python, Java, C++, Python] Set from List: [Java, C++, Python]
Explanation: The addAll() method adds every element of the List to the HashSet. Since the destination collection is a Set, duplicate elements are not stored.