Listnumbers = Arrays.asList(4, 2, 1, 5, 3); TreeSet sortedNumbers = numbers.stream().collect(Collectors.toCollection(TreeSet::new));
String[] words = {"apple", "banana", "cherry", "date", "elderberry"}; LinkedListIn this example, we have an array of strings which we want to collect into a linked list. We use the toCollection method with a LinkedList::new supplier to create a new linked list and collect the stream elements into it. The java.util.stream.Collectors package library provides various methods to perform different kinds of operations on streams using collectors.linkedWords = Arrays.stream(words).collect(Collectors.toCollection(LinkedList::new));