What is the easiest way to convert a List to a Set in Java?
What is the easiest way to convert a List to a Set in Java?
Java- addAll
set.addAll(aList);
Java- new Object
new HashSet<>(list)
Java-8
list.stream().collect(Collectors.toSet());
Using Guva
Sets.newHashSet(list)
Apache Commons
CollectionUtils.addAll(targetSet, sourceList);
Java 10
var set = Set.copyOf(list);
Remember that, converting from List to Set will remove duplicates from collection because List supports duplicates but Set does not support duplicates in Java.
Direct Conversion : The most common and simple way to convert a List to a Set
// Creating a list of strings
List<String> list = Arrays.asList("One", "Two", "Three", "Four");
// Converting a list to set
Set<String> set = new HashSet<>(list);
Apache Commons Collections : You may also use the Commons Collections API to convert a List to a Set :-
// Creating a list of strings
List<String> list = Arrays.asList("One", "Two", "Three", "Four");
// Creating a set with the same number of members in the list
Set<String> set = new HashSet<>(4);
// Adds all of the elements in the list to the target set
CollectionUtils.addAll(set, list);
Using Stream : Another way is to convert given list to stream, then stream to set :-
// Creating a list of strings
List<String> list = Arrays.asList("One", "Two", "Three", "Four");
// Converting to set using stream
Set<String> set = list.stream().collect(Collectors.toSet());
The best way to use constructor
Set s= new HashSet(list);
In java 8 you can also use stream api::
Set s= list.stream().collect(Collectors.toSet());
A more Java 8 resilient solution with Optional.ofNullable
Set<Foo> mySet = Optional.ofNullable(myList).map(HashSet::new).orElse(null);
If you use Eclipse Collections:
MutableSet<Integer> mSet = Lists.mutable.with(1, 2, 3).toSet();
MutableIntSet mIntSet = IntLists.mutable.with(1, 2, 3).toSet();
The MutableSet interface extends java.util.Set whereas the MutableIntSet interface does not. You can also convert any Iterable to a Set using the Sets factory class.
Set<Integer> set = Sets.mutable.withAll(List.of(1, 2, 3));
There is more explanation of the mutable factories available in Eclipse Collections here.
If you want an ImmutableSet from a List, you can use the Sets factory as follows:
ImmutableSet<Integer> immutableSet = Sets.immutable.withAll(List.of(1, 2, 3))
Note: I am a committer for Eclipse Collections
In Java 1.8, the stream API can be used to convert a list to set. For example, the code below shows the conversion of a list to a set:
List<String> empList = Arrays.asList("java", "python", ".net", "javaScript", "php");
Set<String> set = empList.stream().collect(Collectors.toSet());
set.forEach(value -> System.out.printf("%s ", value));
In case the list contains objects and I want to create a set out of it:
List<Employee> empList = Arrays.asList(new Employee(1, 1000, "Chandra Shekhar", 6000),
new Employee(2, 1000, "Rajesh", 8000), new Employee(3, 1004, "Rahul", 9000),
new Employee(4, 1001, "Suresh", 12000), new Employee(5, 1004, "Satosh", 7000));
Set<String> set = empList.stream().map(emp -> emp.getName()).collect(Collectors.toSet());
System.out.println(set);