java - Stream<String> to Map<String, Integer> -


i have stream<string> of file, want combine equal words map<string, integer> counts, how word in stream<string>.

i know have use collect(collectors.groupingby(..)), not know how use it.

it nice, if there can provide hints how solve problem!

it's quite easy create map<string, long> using collectors.counting() downstream collector:

stream<string> s = stream.of("aaa", "bb", "cc", "aaa", "dd");  map<string, long> map = s.collect(collectors.groupingby(         function.identity(), collectors.counting())); 

if don't long type, can count integer way:

map<string, integer> mapint = s.collect(collectors.groupingby(         function.identity(),         collectors.reducing(0, str -> 1, integer::sum))); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -