From 5cbde1b45635b63f8302476e0ceadada01f6123a Mon Sep 17 00:00:00 2001 From: "ray.j.greenwell" Date: Tue, 21 Sep 2010 02:11:03 +0000 Subject: [PATCH] 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 --- .../com/samskivert/util/CollectionUtil.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/com/samskivert/util/CollectionUtil.java b/src/main/java/com/samskivert/util/CollectionUtil.java index ceae52f1..20b9f3a0 100644 --- a/src/main/java/com/samskivert/util/CollectionUtil.java +++ b/src/main/java/com/samskivert/util/CollectionUtil.java @@ -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 > C addAll ( + C col, Iterable> values) + { + for (Collection val : values) { + col.addAll(val); + } + return col; + } + + /** + * Folds all the specified values into the supplied map and returns it. + */ + public static > M putAll ( + M map, Iterable> values) + { + for (Map val : values) { + map.putAll(val); + } + return map; + } + /** * Modify the specified Collection so that only the first limit elements * remain, as determined by iteration order. If the Collection is smaller than limit,