Checkpoint: The Word-Stats Pipeline
beginner40 min readLesson 55 of 180
groupingBy, a max-then-filter pass, and an Optional that means empty when it says empty.
The word-stats pipeline - three stream stages composing into one record.
Inside the provided Solution skeleton, implement analyze(words) as the
checkpoint specifies. Three design notes before you start:
- Lowercase before grouping -
groupingByaftermap(String::toLowerCase), and filter out empty words first (an empty word has no first letter). - Two passes are idiomatic -
max()thenfilter(length == max)is clearer than a clever single-pass fold, and streams are cheap to run twice on an in-memory list. - Empty input -
String.joinover nothing yields""; the spec wants an empty Optional, so guard it. This is exactly the boundary honesty thatOptionalexists for.
The wrong solution shown in the notes fails two ways: it builds
Optional.of("") for empty input, and it crashes on empty input before
that - substring(0, 1) has no first letter to take.