JavaBeginner#streams

What is the difference between Collectors.toList() and Collectors.toSet()?

toList() collects stream elements into a List preserving encounter order and duplicates. toSet() collects into a Set, removing duplicates and with no guaranteed order (unless a specific Set supplier is used).

Example
Stream.of(1,2,2,3).collect(Collectors.toList()); // [1,2,2,3]
Stream.of(1,2,2,3).collect(Collectors.toSet());  // [1,2,3] order not guaranteed

Related Questions

1
JavaIntermediate#streams

How do you group elements using Collectors.groupingBy?

Open
2
JavaIntermediate#streams

What is the difference between findFirst() and findAny() in streams?

Open
3
JavaBeginner#java8

What is the difference between Optional.of() and Optional.ofNullable()?

Open