guava - Create Multimap from Map<K,Collection<V>> with key as V[0] and value as K using Java 8 -
i have map<string,list<string>>
(say inputmap) , want convert map<string,list<string>>
each (k,v) in new map (v.get(0),k) of inputmap.
ex.
x -> b,c,d y -> b,d,e z -> b,g,h p -> a,b,d q -> a,d,f r -> a,c,b
to
b->x,y,z a->p,q,r
i thought using like
inputmap.entryset().stream().collect(collectors.tomap(map.entry::getvalue.get(0),map.entry::getkey));
and converting map multimap, cannot write map.entry::getvalue.get(0)
it great if create multimap in .collect()
itself.
you can collect directly multimap
:
multimap<string, string> result = inputmap.entryset().stream() .collect( arraylistmultimap::create, (mm,e) -> mm.put(e.getvalue().get(0), e.getkey()), multimap::putall );
Comments
Post a Comment