List of usage examples for com.google.common.collect Maps.EntryTransformer transformEntry
static <V2, K, V1> Entry<K, V2> transformEntry(final EntryTransformer<? super K, ? super V1, V2> transformer, final Entry<K, V1> entry)
From source file:edu.mit.streamjit.util.CollectionUtils.java
/** * Returns the union of the given maps, using the given function to merge * values for the same key. The function is called for all keys with a list * of the values of the maps in the order the maps were given. Maps that do * not contain the key are not represented in the list. The function's * return value is used as the value in the union map. * TODO: the generics don't seem right here; I should be able to use e.g. * a Collection<Comparable> in place of List<Integer> for the middle arg. * Note that the above overload permits that and forwards to this one! * @param <K> the key type of the returned map * @param <V> the value type of the returned map * @param <X> the value type of the input map(s) * @param merger the function used to merge values for the same key * @param maps the maps/*from w ww. j ava 2 s . c o m*/ * @return a map containing all the keys in the given maps */ public static <K, V, X> ImmutableMap<K, V> union( Maps.EntryTransformer<? super K, ? super List<X>, ? extends V> merger, List<? extends Map<? extends K, ? extends X>> maps) { ImmutableSet.Builder<K> keys = ImmutableSet.builder(); for (Map<? extends K, ? extends X> m : maps) keys.addAll(m.keySet()); ImmutableMap.Builder<K, V> builder = ImmutableMap.builder(); for (K k : keys.build()) { ImmutableList.Builder<X> values = ImmutableList.builder(); for (Map<? extends K, ? extends X> m : maps) if (m.containsKey(k)) values.add(m.get(k)); builder.put(k, merger.transformEntry(k, values.build())); } return builder.build(); }