public class Histogram {
public static void main(String[] args) throws IOException {
Stream<String> stream = Files.lines(Paths.get("words.txt"));
ArrayList<String> list = (ArrayList<String>) stream
.map(w -> w.split("\\.|\\s+|,")).flatMap(Arrays::stream)
.filter(x-> x.length() != 0)
.collect(Collectors.toList());
list.forEach(s -> System.out.print(s + "(" + s.length() + ") "));
}
}
Now I have all results in one line.
How I can make logic in this Java stream 8, to put every 5 words from forEach loop to newline (System.out.println
).
Every sixth word is printed in a new line.