functional programming - Collect Lines using Multimap Collector -
is there way covert below using collectors yet?
list<string[]> lines = getlines(); multimap<string,string> multimap = arraylistmultimap.create(); lines.foreach(line -> multimap.put(line[0],line[1]); );
you can use multimaps.tomultimap
collector:
listmultimap<string, string> multimap = lines.stream() .collect(multimaps.tomultimap( l -> l[0], l -> l[1], arraylistmultimap::create ));
or if don't need mutability, use immutablelistmultimap.toimmutablelistmultimap
collector:
listmultimap<string, string> multimap = lines.stream() .collect(toimmutablelistmultimap(l -> l[0], l -> l[1]));
Comments
Post a Comment