Java Streams | groupingBy same elements -


i have stream of words , sort them according occurrence of same elements (=words).

e.g.: {hello, world, hello}

to

map<string, list<string>> 

hello, {hello, hello}

world, {world}

what have far:

map<object, list<string>> list = streamofwords.collect(collectors.groupingby(???)); 

problem 1: stream seems lose information processing strings, therefore compiler forces me change type object, list

problem 2: don't know put inside parentesis group same occurrence. know able process single elements within th lambda-expression have no idea how reach "outside" each element check equality.

thank you

the keyextractor searching identity function:

map<string, list<string>> list = streamofwords.collect(collectors.groupingby(function.identity())); 

edit added explanation:

  • function.identity() retuns 'function' 1 method nothing more returning argument gets.
  • collectors.groupingby(function<s, k> keyextractor) provides collector, collects elements of stream map<k, list<s>>. using keyextractor implementation gets inspect stream's objects of type s , deduce key of type k them. key map's key used (or create) list in result map stream element added to.

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 -