Java How to Convert List to Set - Simple Example
To convert a list to a set in Java, create a new
HashSet by passing the list to its constructor like Set<Type> set = new HashSet<>(list);.Examples
Input[1, 2, 3, 2]
Output[1, 2, 3]
Input["apple", "banana", "apple"]
Output[apple, banana]
Input[]
Output[]
How to Think About It
To convert a list to a set, think about removing duplicates because a set only keeps unique items. You can do this by creating a set and giving it the list, so it automatically filters out repeated elements.
Algorithm
1
Get the input list.2
Create a new empty set.3
Add all elements from the list to the set.4
Return the set with unique elements.Code
java
import java.util.*; public class ListToSet { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 2); Set<Integer> set = new HashSet<>(list); System.out.println(set); } }
Output
[1, 2, 3]
Dry Run
Let's trace converting list [1, 2, 3, 2] to a set.
1
Start with list
list = [1, 2, 3, 2]
2
Create set from list
set = new HashSet<>(list) -> set = [1, 2, 3]
3
Print set
Output: [1, 2, 3]
| Iteration | Set Content |
|---|---|
| After adding 1 | [1] |
| After adding 2 | [1, 2] |
| After adding 3 | [1, 2, 3] |
| After adding 2 again | [1, 2, 3] |
Why This Works
Step 1: Why use HashSet constructor
Passing the list to the HashSet constructor copies all elements and removes duplicates automatically.
Step 2: Sets store unique elements
A Set does not allow duplicates, so repeated items in the list are ignored.
Step 3: Result is unique collection
The resulting set contains only one copy of each element from the list.
Alternative Approaches
Using addAll() method
java
import java.util.*; public class ListToSetAlt { public static void main(String[] args) { List<String> list = Arrays.asList("apple", "banana", "apple"); Set<String> set = new HashSet<>(); set.addAll(list); System.out.println(set); } }
This approach creates an empty set first and then adds all list elements; it is more flexible if you want to add elements later.
Using Java 8 Streams
java
import java.util.*; import java.util.stream.Collectors; public class ListToSetStream { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 2); Set<Integer> set = list.stream().collect(Collectors.toSet()); System.out.println(set); } }
Streams provide a modern, readable way to convert lists to sets but may be less straightforward for beginners.
Complexity: O(n) time, O(n) space
Time Complexity
Creating a set from a list takes O(n) time because it processes each element once to add it to the set.
Space Complexity
The set requires O(n) space to store unique elements from the list.
Which Approach is Fastest?
Using the HashSet constructor or addAll() method both run in O(n) time; streams add slight overhead but offer more flexibility.
| Approach | Time | Space | Best For |
|---|---|---|---|
| HashSet constructor | O(n) | O(n) | Simple and fast conversion |
| addAll() method | O(n) | O(n) | When set needs to be created empty first |
| Java 8 Streams | O(n) | O(n) | Modern style, chaining operations |
Use
new HashSet<>(list) for a quick and simple conversion from list to set.Trying to assign a list directly to a set variable without conversion causes a type error.