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

I was able to do this by following these steps:

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 with Guava?

+4
source share
3 answers

No, this was denied, see The idea of ​​a cemetery :

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

Please note that we can add ImmutableMap.copyOf(Iterable<Entry>).

+6
source

Only a little 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();
+1
source
0

All Articles