Ilya Ilya - 1 month ago 10
Java Question

Java streams collect list of objects to buckets

I have a problem of collecting some list values to buckets. For example, let's assume I have a list of Strings:

List<String> strs = Arrays.asList("ABC", "abc", "bca", "BCa", "AbC");

And I want to put the strings into set (or list) of sets, that contain only case-different strings, i.e. for example above it would be collection of two sets:
[["ABC", "abc", "AbC"], ["bca", "BCa"]]

So help me please to write collector for this problem.

List<Set<String>> result =
.collect(/* some collectors magic here */)


The "some collectors magic" you are looking for can be done in two steps:

  • first, you need to group the elements by the property you are looking for. In this case since you want to ignore the casing, String#toLowerCase does the job (don't forget the overloaded method that takes a Locale as parameter). You also want the values grouped to be unique so you can use the overloaded version of groupingBy to put them into a Set (the default implementation uses a List)
  • since you're not interested in the keys, just grab the values from the resulting map and put them in a list (if you really need a list) using the collectingAndThen collector.

import static;
import static;
import static;


List<Set<String>> result =
        .collect(collectingAndThen(groupingBy(String::toLowerCase, toSet()), 
                                   m -> new ArrayList<>(m.values())));