Did you know that you can navigate the posts by swiping left and right?

Using grouping collectors in java 8

02 Jan 2016 . code . Comments #java #java8 #lambda #functional #code

Java 8 can group elements in a stream based on a provided lambda using a collector.

This is easy by using Collectors.groupBy


Stream.of(...).collect(Collectors.groupBy(...))

The example below does the following:

  1. Creates a stream of words
  2. Groups them by the letter they start with
  3. Puts the groups into a TreeMap
  4. Collects the individual groups using TreeSets

You can also check this more complete example with variations on how to call Collectors.groupBy, or find all my java 8 excercises in this repo: java8-examples