How do I use Collectors.toCollection() method?

The Collectors.toCollection() method is a static method in the java.util.stream.Collectors class of Java 8. This method is used with streams when you want to convert a list to another collection type.

Here’s a simple example of how to use the method:

package org.kodejava.stream;

import java.util.*;
import java.util.stream.Collectors;

public class CollectorsToCollection {
    public static void main(String[] args) {
        List<String> list = 
                Arrays.asList("Java", "Kotlin", "Python", "Scala", "Kotlin");

        // Convert List to TreeSet
        TreeSet<String> treeSet = list.stream()
                .collect(Collectors.toCollection(TreeSet::new));

        System.out.println(treeSet);
    }
}

Output:

[Java, Kotlin, Python, Scala]

In this code:

  • We have a List of Strings.
  • We convert this list into a TreeSet.
  • Collectors.toCollection(TreeSet::new) is the collector that collects the data from the stream into a new TreeSet.
  • The method referenced by TreeSet::new is a constructor reference that creates a new empty TreeSet.

The output of the program will be the TreeSet containing the elements of the list.

Keep in mind that a TreeSet automatically orders its elements (in this case, alphabetically since the elements are Strings) and does not allow duplicates. So, if the list had duplicate values, and you wanted to maintain them in your new collection, you would need to choose a different type of Set or use a List.

How do I get the last element of SortedSet?

package org.kodejava.util;

import java.util.SortedSet;
import java.util.TreeSet;

public class LastSetElement {
    public static void main(String[] args) {
        SortedSet<String> numbers = new TreeSet<>();
        numbers.add("One");
        numbers.add("Two");
        numbers.add("Three");
        numbers.add("Four");
        numbers.add("Five");

        // SortedSet orders the items it contains. We can get the last
        // item from the set using the last() method. The last item 
        // will be "Two".
        String lastElement = numbers.last();
        System.out.println("lastElement = " + lastElement);
    }
}

How do I get the first element of SortedSet?

package org.kodejava.util;

import java.util.TreeSet;
import java.util.SortedSet;

public class FirstSetElement {
    public static void main(String[] args) {
        SortedSet<String> numbers = new TreeSet<>();
        numbers.add("One");
        numbers.add("Two");
        numbers.add("Three");
        numbers.add("Four");
        numbers.add("Five");

        // SortedSet orders the items it contains. We can get the first
        // item from the SortedSet using the first() method. The fist item 
        // will be "Five".
        String firstElement = numbers.first();
        System.out.println("firstElement = " + firstElement);
    }
}

How do I sort items in a Set?

The trick to sort a java.util.Set is to use the implementation of a java.util.SortedSet such as the java.util.TreeSet class. The example below shows you the result of using the java.util.TreeSet class, in which the items in it will be sorted based on the element’s natural order.

package org.kodejava.util;

import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        // The TreeSet class is an implementation of a SortedSet, this means
        // that when you are using the TreeSet to store you data collections
        // you'll get the items ordered base on its elements natural order.
        Set<String> set = new TreeSet<>();

        // In the example below we add some letters to the TreeSet, this mean
        // that the alphabets will be ordered based on the alphabet order
        // which is from A to Z.
        set.add("Z");
        set.add("A");
        set.add("F");
        set.add("B");
        set.add("H");
        set.add("X");
        set.add("N");

        for (String item : set) {
            System.out.print(item + " ");
        }
    }
}

This demo prints:

A B F H N X Z