← All Exercises

Java Stream Collect

intermediatejava~20min

Background

Java Stream API provides declarative data processing with collectors for grouping, partitioning, and reducing.

Problem

Write a Java method that takes a list of strings and groups them by length using Stream groupingBy collector.

Input

A list of strings.

Output

A map from string length to list of strings of that length.

Sample tests

Sample 1

Input: ["a","bb","ccc","dd","e"]
Expected: {1:["a","e"], 2:["bb","dd"], 3:["ccc"]}

Your solution

Learning objective

Learn Stream collect, groupingBy collector, and method references for classification functions.