Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform Iterable<Map.Entry<A,B>> to Map<A,B> using Guava?

Tags:

java

guava

I was able to do that by doing the following :

Iterable<Map.Entry<A,B>> entryIterable
Map<A, B> aBMap = newHashMap();
for (Map.Entry<A, B> aBEntry : entryIterable) {
   aBMap.put(aBEntry.getKey() , aBEntry.getValue());
}

Is there an easier way to do this using Guava?

like image 572
mota Avatar asked Feb 15 '23 17:02

mota


2 Answers

No, this was refused, see the Idea Graveyard:

Create a map from an Iterable<Pair>, Iterable<Map.Entry>, Object[] (alternating keys and values), or from List<K> + List<V>

Note that we may still add ImmutableMap.copyOf(Iterable<Entry>).

like image 151
maaartinus Avatar answered Feb 17 '23 11:02

maaartinus


Only a wee bit easier:

ImmutableMap.Builder<A, B> builder = ImmutableMap.builder();
for (Map.Entry<A, B> entry : entries) {
  builder.put(entry); // no getKey(), no getValue()
}
return builder.build();
like image 28
Louis Wasserman Avatar answered Feb 17 '23 09:02

Louis Wasserman