Sean Parsons
unread,Aug 22, 2008, 7:54:43 AM8/22/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Google Collections Library - developer list
Internally we're using this method based off the back of the existing
Google Collections work and I've been given permission to share it in
the spirit of the library I guess you could say:
public static <K, V> void addAll(Map<K,V> destinationMap,
Iterable<V> source, Function<V, K> valueToKeyFunction)
{
Preconditions.checkArgument(destinationMap != null,
"Destination map cannot be null.");
Preconditions.checkArgument(source != null, "Iterable source
cannot be null.");
Preconditions.checkArgument(valueToKeyFunction != null, "Value
converting function cannot be null.");
destinationMap.putAll(Maps.uniqueIndex(source,
valueToKeyFunction));
//for (V value : iterable)
//{
// map.put(valueToKeyFunction.apply(value), value);
//}
}
The commented out code at the bottom is the original version, I've not
done any particular performance testing. But it seems like a
reasonable assumption that the two methods used in the current
revision could offer some performance benefits over pure iteration.
Sean.