We've already seen the implementations of Collectors.toList(). The Collectors.toList() method helps collect the elements of a Stream into a List. The important thing to note here is that you can't specify which List implementation to use; instead, it'll always use the default one.
Collectors.toSet() is similar to the Collectors.toList() method, just instead of List, it repackages the elements into a set. Again, with Collectors.toSet(), you won't be able to specify which set implementation to use.
The Collectors.toCollection() method is a complementing version of the toList() and toSet(); it lets you provide a custom Collection to accumulate the list into.
Consider the following example to explain it:
fun main(args: Array<String>) { val resultantSet = (0..10).asSequence().asStream() .collect(Collectors.toCollection{LinkedHashSet<Int>()}) println("resultantSet $resultantSet") }
The output is as follows: