Answer several questions about a text, each with its own stream pipeline.
What to build
A program that reports word statistics using streams.
Requirements
- Read lines until the input ends; words are runs of letters, in lower case.
- Print
words: N(all words),distinct: D,longest: W(the longest word, alphabetically first on a tie, ornone),by length: ...(the distinct words sorted by length, then alphabetically, separated by spaces), andletters: a=2 b=1 ...(the number of words, counting repeats, starting with each letter, in alphabetical order).
Input
Any number of lines of text.
Output
Five lines.
Constraints
At most 2000 words. If there are no words, print words: 0, distinct: 0, longest: none, by length: and letters:.
Example
Input:
The cat sat on the mat.
A cat!
Output:
words: 8
distinct: 6
longest: cat
by length: a on cat mat sat the
letters: a=1 c=2 m=1 o=1 s=1 t=2
Why it works
Each question is a short pipeline over the same list of words: count, distinct, a comparator for the longest and the ordering, and a grouping collector for the first letters.
Examples
Example 1 · Input
The cat sat on the mat. A cat!
Expected output
words: 8 distinct: 6 longest: cat by length: a on cat mat sat the letters: a=1 c=2 m=1 o=1 s=1 t=2
Example 2 · Input
Hello, hello world
Expected output
words: 3 distinct: 2 longest: hello by length: hello world letters: h=2 w=1
Hints
Hint 1
Split on non-letters, lower-case, and filter out empty strings before anything else.
Hint 2
groupingBy(w -> w.substring(0, 1), TreeMap::new, counting()) counts words by first letter.
Solution
Show one correct solution (try it yourself first)
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<String> words = new ArrayList<>();
while (in.hasNextLine()) {
Arrays.stream(in.nextLine().toLowerCase().split("[^a-z]+")).filter(w -> !w.isEmpty()).forEach(words::add);
}
Comparator<String> byLength = Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder());
List<String> distinct = words.stream().distinct().sorted(byLength).collect(Collectors.toList());
String longest = words.stream().sorted(Comparator.comparingInt(String::length).reversed().thenComparing(Comparator.naturalOrder())).findFirst().orElse("none");
Map<String, Long> letters = words.stream().collect(Collectors.groupingBy(w -> w.substring(0, 1), TreeMap::new, Collectors.counting()));
String letterText = letters.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(" "));
System.out.println("words: " + words.size());
System.out.println("distinct: " + distinct.size());
System.out.println("longest: " + longest);
System.out.println("by length: " + String.join(" ", distinct));
System.out.println("letters: " + letterText);
}
}