Created an addAll() that folds a supplied Iterable of Collections

into the starter collection using addAll. Certain Collections, like
guava's Multiset have optimized addAll() methods that recognize
other Multisets.
There is also a putAll() equivalent for Maps.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2891 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray.j.greenwell
2010-09-21 02:11:03 +00:00
parent ace6b104e2
commit 5cbde1b456
@@ -28,6 +28,7 @@ import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import com.samskivert.annotation.ReplacedBy;
@@ -79,6 +80,30 @@ public class CollectionUtil
return col;
}
/**
* Folds all the specified values into the supplied collection and returns it.
*/
public static <T, C extends Collection<T>> C addAll (
C col, Iterable<? extends Collection<? extends T>> values)
{
for (Collection<? extends T> val : values) {
col.addAll(val);
}
return col;
}
/**
* Folds all the specified values into the supplied map and returns it.
*/
public static <K, V, M extends Map<K, V>> M putAll (
M map, Iterable<? extends Map<? extends K, ? extends V>> values)
{
for (Map<? extends K, ? extends V> val : values) {
map.putAll(val);
}
return map;
}
/**
* Modify the specified Collection so that only the first <code>limit</code> elements
* remain, as determined by iteration order. If the Collection is smaller than limit,