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 streammap<k, list<s>>
. using keyextractor implementation gets inspect stream's objects of types
, deduce key of typek
them. key map's key used (or create) list in result map stream element added to.
Comments
Post a Comment