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?
No, this was refused, see the Idea Graveyard:
Create a map from an
Iterable<Pair>
,Iterable<Map.Entry>
,Object[]
(alternating keys and values), or fromList<K>
+List<V>
Note that we may still add
ImmutableMap.copyOf(Iterable<Entry>)
.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With