Java Lambda 计算每个单词出现的次数
String words = "If you miss the train I'm on You will know that I am gone You can hear the whistle blow a hundred miles A hundred miles a hundred miles";
Map<String, Long> map = Stream.of(words).map(word -> word.split(" ")).flatMap(Stream::of).collect(Collectors.groupingBy(word -> word, Collectors.counting()));
map.forEach((key, value) -> {
System.out.println(key + " " + value);
});
A 1
a 2
hundred 3
will 1
whistle 1
I 1
am 1
I'm 1
miles 3
gone 1
miss 1
the 2
can 1
that 1
know 1
blow 1
If 1
hear 1
You 2
you 1
on 1
train 1