一、前言
通过apache-commons包中的org.apache.commons.collections.CollectionUtils集合操作工具类对集合间进行合并union、交叉intersection、分离disjunction、减去subtract、任意包含containsAny、判断是否为子集isSubCollection、颠倒序列reverseArray及判断是否填满isFull等操作。
二、源码说明
package org.apache.commons.collections;@b@@b@import java.lang.reflect.Array;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.Enumeration;@b@import java.util.HashMap;@b@import java.util.HashSet;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.ListIterator;@b@import java.util.Map;@b@import java.util.Set;@b@import org.apache.commons.collections.collection.PredicatedCollection;@b@import org.apache.commons.collections.collection.SynchronizedCollection;@b@import org.apache.commons.collections.collection.TransformedCollection;@b@import org.apache.commons.collections.collection.TypedCollection;@b@import org.apache.commons.collections.collection.UnmodifiableBoundedCollection;@b@import org.apache.commons.collections.collection.UnmodifiableCollection;@b@@b@public class CollectionUtils@b@{@b@ private static Integer INTEGER_ONE = new Integer(1);@b@ public static final Collection EMPTY_COLLECTION = UnmodifiableCollection.decorate(new ArrayList());@b@@b@ public static Collection union(Collection a, Collection b)@b@ {@b@ Object obj;@b@ int i;@b@ ArrayList list = new ArrayList();@b@ Map mapa = getCardinalityMap(a);@b@ Map mapb = getCardinalityMap(b);@b@ Set elts = new HashSet(a);@b@ elts.addAll(b);@b@ Iterator it = elts.iterator();@b@ while (it.hasNext()) {@b@ obj = it.next();@b@ i = 0; for (int m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; ++i)@b@ list.add(obj);@b@ }@b@@b@ return list;@b@ }@b@@b@ public static Collection intersection(Collection a, Collection b)@b@ {@b@ Object obj;@b@ int i;@b@ ArrayList list = new ArrayList();@b@ Map mapa = getCardinalityMap(a);@b@ Map mapb = getCardinalityMap(b);@b@ Set elts = new HashSet(a);@b@ elts.addAll(b);@b@ Iterator it = elts.iterator();@b@ while (it.hasNext()) {@b@ obj = it.next();@b@ i = 0; for (int m = Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; ++i)@b@ list.add(obj);@b@ }@b@@b@ return list;@b@ }@b@@b@ public static Collection disjunction(Collection a, Collection b)@b@ {@b@ Object obj;@b@ int i;@b@ ArrayList list = new ArrayList();@b@ Map mapa = getCardinalityMap(a);@b@ Map mapb = getCardinalityMap(b);@b@ Set elts = new HashSet(a);@b@ elts.addAll(b);@b@ Iterator it = elts.iterator();@b@ while (it.hasNext()) {@b@ obj = it.next();@b@ i = 0; for (int m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)) - Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; ++i)@b@ list.add(obj);@b@ }@b@@b@ return list;@b@ }@b@@b@ public static Collection subtract(Collection a, Collection b)@b@ {@b@ ArrayList list = new ArrayList(a);@b@ for (Iterator it = b.iterator(); it.hasNext(); )@b@ list.remove(it.next());@b@@b@ return list;@b@ }@b@@b@ public static boolean containsAny(Collection coll1, Collection coll2)@b@ {@b@ Iterator it;@b@ if (coll1.size() < coll2.size()) {@b@ it = coll1.iterator();@b@ do if (!(it.hasNext())) break label48;@b@ while (!(coll2.contains(it.next())));@b@ label48: return true;@b@ }@b@ else@b@ {@b@ it = coll2.iterator();@b@ do if (!(it.hasNext())) break label84;@b@ while (!(coll1.contains(it.next())));@b@ return true;@b@ }@b@@b@ label84: return false;@b@ }@b@@b@ public static Map getCardinalityMap(Collection coll)@b@ {@b@ Map count = new HashMap();@b@ for (Iterator it = coll.iterator(); it.hasNext(); ) {@b@ Object obj = it.next();@b@ Integer c = (Integer)(Integer)count.get(obj);@b@ if (c == null)@b@ count.put(obj, INTEGER_ONE);@b@ else@b@ count.put(obj, new Integer(c.intValue() + 1));@b@ }@b@@b@ return count;@b@ }@b@@b@ public static boolean isSubCollection(Collection a, Collection b)@b@ {@b@ Map mapa = getCardinalityMap(a);@b@ Map mapb = getCardinalityMap(b);@b@ Iterator it = a.iterator();@b@ while (it.hasNext()) {@b@ Object obj = it.next();@b@ if (getFreq(obj, mapa) > getFreq(obj, mapb))@b@ return false;@b@ }@b@@b@ return true;@b@ }@b@@b@ public static boolean isProperSubCollection(Collection a, Collection b)@b@ {@b@ return ((a.size() < b.size()) && (isSubCollection(a, b)));@b@ }@b@@b@ public static boolean isEqualCollection(Collection a, Collection b)@b@ {@b@ if (a.size() != b.size())@b@ return false;@b@@b@ Map mapa = getCardinalityMap(a);@b@ Map mapb = getCardinalityMap(b);@b@ if (mapa.size() != mapb.size())@b@ return false;@b@@b@ Iterator it = mapa.keySet().iterator();@b@ while (it.hasNext()) {@b@ Object obj = it.next();@b@ if (getFreq(obj, mapa) != getFreq(obj, mapb))@b@ return false;@b@ }@b@@b@ return true;@b@ }@b@@b@ public static int cardinality(Object obj, Collection coll)@b@ {@b@ Iterator it;@b@ if (coll instanceof Set)@b@ return ((coll.contains(obj)) ? 1 : 0);@b@@b@ if (coll instanceof Bag)@b@ return ((Bag)coll).getCount(obj);@b@@b@ int count = 0;@b@ if (obj == null) {@b@ it = coll.iterator();@b@ while (true) { do if (!(it.hasNext())) break label78;@b@ while (it.next() != null);@b@ label78: ++count;@b@ }@b@ }@b@ else {@b@ it = coll.iterator();@b@ while (true) { do if (!(it.hasNext())) break label116;@b@ while (!(obj.equals(it.next())));@b@ ++count;@b@ }@b@ }@b@@b@ label116: return count;@b@ }@b@@b@ public static Object find(Collection collection, Predicate predicate)@b@ {@b@ Iterator iter;@b@ if ((collection != null) && (predicate != null))@b@ for (iter = collection.iterator(); iter.hasNext(); ) {@b@ Object item = iter.next();@b@ if (predicate.evaluate(item))@b@ return item;@b@ }@b@@b@@b@ return null;@b@ }@b@@b@ public static void forAllDo(Collection collection, Closure closure)@b@ {@b@ Iterator it;@b@ if ((collection != null) && (closure != null))@b@ for (it = collection.iterator(); it.hasNext(); )@b@ closure.execute(it.next());@b@ }@b@@b@ public static void filter(Collection collection, Predicate predicate)@b@ {@b@ if ((collection != null) && (predicate != null)) {@b@ Iterator it = collection.iterator();@b@ while (true) { do if (!(it.hasNext())) return;@b@ while (predicate.evaluate(it.next()));@b@ it.remove();@b@ }@b@ }@b@ }@b@@b@ public static void transform(Collection collection, Transformer transformer)@b@ {@b@ if ((collection != null) && (transformer != null)) {@b@ ListIterator it;@b@ if (collection instanceof List) {@b@ List list = (List)collection;@b@ for (it = list.listIterator(); it.hasNext(); )@b@ it.set(transformer.transform(it.next()));@b@ }@b@ else {@b@ Collection resultCollection = collect(collection, transformer);@b@ collection.clear();@b@ collection.addAll(resultCollection);@b@ }@b@ }@b@ }@b@@b@ public static int countMatches(Collection inputCollection, Predicate predicate)@b@ {@b@ int count = 0;@b@ if ((inputCollection != null) && (predicate != null)) {@b@ Iterator it = inputCollection.iterator();@b@ while (true) { do if (!(it.hasNext())) break label47;@b@ while (!(predicate.evaluate(it.next())));@b@ ++count;@b@ }@b@ }@b@@b@ label47: return count;@b@ }@b@@b@ public static boolean exists(Collection collection, Predicate predicate)@b@ {@b@ if ((collection != null) && (predicate != null)) {@b@ Iterator it = collection.iterator();@b@ do if (!(it.hasNext())) break label41;@b@ while (!(predicate.evaluate(it.next())));@b@ return true;@b@ }@b@@b@ label41: return false;@b@ }@b@@b@ public static Collection select(Collection inputCollection, Predicate predicate)@b@ {@b@ ArrayList answer = new ArrayList(inputCollection.size());@b@ select(inputCollection, predicate, answer);@b@ return answer;@b@ }@b@@b@ public static void select(Collection inputCollection, Predicate predicate, Collection outputCollection)@b@ {@b@ Iterator iter;@b@ if ((inputCollection != null) && (predicate != null))@b@ for (iter = inputCollection.iterator(); iter.hasNext(); ) {@b@ Object item = iter.next();@b@ if (predicate.evaluate(item))@b@ outputCollection.add(item);@b@ }@b@ }@b@@b@ public static Collection selectRejected(Collection inputCollection, Predicate predicate)@b@ {@b@ ArrayList answer = new ArrayList(inputCollection.size());@b@ selectRejected(inputCollection, predicate, answer);@b@ return answer;@b@ }@b@@b@ public static void selectRejected(Collection inputCollection, Predicate predicate, Collection outputCollection)@b@ {@b@ Iterator iter;@b@ if ((inputCollection != null) && (predicate != null))@b@ for (iter = inputCollection.iterator(); iter.hasNext(); ) {@b@ Object item = iter.next();@b@ if (!(predicate.evaluate(item)))@b@ outputCollection.add(item);@b@ }@b@ }@b@@b@ public static Collection collect(Collection inputCollection, Transformer transformer)@b@ {@b@ ArrayList answer = new ArrayList(inputCollection.size());@b@ collect(inputCollection, transformer, answer);@b@ return answer;@b@ }@b@@b@ public static Collection collect(Iterator inputIterator, Transformer transformer)@b@ {@b@ ArrayList answer = new ArrayList();@b@ collect(inputIterator, transformer, answer);@b@ return answer;@b@ }@b@@b@ public static Collection collect(Collection inputCollection, Transformer transformer, Collection outputCollection)@b@ {@b@ if (inputCollection != null)@b@ return collect(inputCollection.iterator(), transformer, outputCollection);@b@@b@ return outputCollection;@b@ }@b@@b@ public static Collection collect(Iterator inputIterator, Transformer transformer, Collection outputCollection)@b@ {@b@ while ((inputIterator != null) && (transformer != null) && @b@ (inputIterator.hasNext())) {@b@ Object item = inputIterator.next();@b@ Object value = transformer.transform(item);@b@ outputCollection.add(value);@b@ }@b@@b@ return outputCollection;@b@ }@b@@b@ public static boolean addIgnoreNull(Collection collection, Object object)@b@ {@b@ return ((object == null) ? false : collection.add(object));@b@ }@b@@b@ public static void addAll(Collection collection, Iterator iterator)@b@ {@b@ while (iterator.hasNext())@b@ collection.add(iterator.next());@b@ }@b@@b@ public static void addAll(Collection collection, Enumeration enumeration)@b@ {@b@ while (enumeration.hasMoreElements())@b@ collection.add(enumeration.nextElement());@b@ }@b@@b@ public static void addAll(Collection collection, Object[] elements)@b@ {@b@ int i = 0; for (int size = elements.length; i < size; ++i)@b@ collection.add(elements[i]);@b@ }@b@@b@ /**@b@ * @deprecated@b@ */@b@ public static Object index(Object obj, int idx)@b@ {@b@ return index(obj, new Integer(idx));@b@ }@b@@b@ /**@b@ * @deprecated@b@ */@b@ public static Object index(Object obj, Object index)@b@ {@b@ if (obj instanceof Map) {@b@ Map map = (Map)obj;@b@ if (map.containsKey(index))@b@ return map.get(index);@b@ }@b@@b@ int idx = -1;@b@ if (index instanceof Integer)@b@ idx = ((Integer)index).intValue();@b@@b@ if (idx < 0)@b@ return obj;@b@@b@ if (obj instanceof Map) {@b@ Map map = (Map)obj;@b@ Iterator iterator = map.keySet().iterator();@b@ return index(iterator, idx);@b@ }@b@ if (obj instanceof List)@b@ return ((List)obj).get(idx);@b@@b@ if (obj instanceof Object[])@b@ return ((Object[])(Object[])obj)[idx];@b@@b@ if (obj instanceof Enumeration) {@b@ Enumeration it = (Enumeration)obj;@b@ while (it.hasMoreElements()) {@b@ --idx;@b@ if (idx == -1)@b@ return it.nextElement();@b@@b@ it.nextElement();@b@ }@b@ }@b@ else {@b@ if (obj instanceof Iterator)@b@ return index((Iterator)obj, idx);@b@@b@ if (obj instanceof Collection) {@b@ Iterator iterator = ((Collection)obj).iterator();@b@ return index(iterator, idx); }@b@ }@b@ return obj;@b@ }@b@@b@ private static Object index(Iterator iterator, int idx) {@b@ while (iterator.hasNext()) {@b@ --idx;@b@ if (idx == -1)@b@ return iterator.next();@b@@b@ iterator.next();@b@ }@b@@b@ return iterator;@b@ }@b@@b@ public static Object get(Object object, int index)@b@ {@b@ if (index < 0)@b@ throw new IndexOutOfBoundsException("Index cannot be negative: " + index);@b@@b@ if (object instanceof Map) {@b@ Map map = (Map)object;@b@ Iterator iterator = map.entrySet().iterator();@b@ return get(iterator, index); }@b@ if (object instanceof List)@b@ return ((List)object).get(index);@b@ if (object instanceof Object[])@b@ return ((Object[])(Object[])object)[index];@b@ if (object instanceof Iterator) {@b@ Iterator it = (Iterator)object;@b@ while (it.hasNext()) {@b@ --index;@b@ if (index == -1)@b@ return it.next();@b@@b@ it.next();@b@ }@b@@b@ throw new IndexOutOfBoundsException("Entry does not exist: " + index); }@b@ if (object instanceof Collection) {@b@ Iterator iterator = ((Collection)object).iterator();@b@ return get(iterator, index); }@b@ if (object instanceof Enumeration) {@b@ Enumeration it = (Enumeration)object;@b@ while (it.hasMoreElements()) {@b@ --index;@b@ if (index == -1)@b@ return it.nextElement();@b@@b@ it.nextElement();@b@ }@b@@b@ throw new IndexOutOfBoundsException("Entry does not exist: " + index); }@b@ if (object == null)@b@ throw new IllegalArgumentException("Unsupported object type: null");@b@ try@b@ {@b@ return Array.get(object, index);@b@ } catch (IllegalArgumentException ex) {@b@ throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());@b@ }@b@ }@b@@b@ public static int size(Object object)@b@ {@b@ int total = 0;@b@ if (object instanceof Map) {@b@ total = ((Map)object).size();@b@ } else if (object instanceof Collection) {@b@ total = ((Collection)object).size();@b@ } else if (object instanceof Object[]) {@b@ total = ((Object[])(Object[])object).length;@b@ } else if (object instanceof Iterator) {@b@ Iterator it = (Iterator)object;@b@ while (it.hasNext()) {@b@ ++total;@b@ it.next();@b@ }@b@ } else if (object instanceof Enumeration) {@b@ Enumeration it = (Enumeration)object;@b@ while (it.hasMoreElements()) {@b@ ++total;@b@ it.nextElement(); }@b@ } else {@b@ if (object == null)@b@ throw new IllegalArgumentException("Unsupported object type: null");@b@ try@b@ {@b@ total = Array.getLength(object);@b@ } catch (IllegalArgumentException ex) {@b@ throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());@b@ }@b@ }@b@ return total;@b@ }@b@@b@ public static boolean sizeIsEmpty(Object object)@b@ {@b@ if (object instanceof Collection)@b@ return ((Collection)object).isEmpty();@b@ if (object instanceof Map)@b@ return ((Map)object).isEmpty();@b@ if (object instanceof Object[])@b@ return (((Object[])(Object[])object).length == 0);@b@ if (object instanceof Iterator)@b@ return (!(((Iterator)object).hasNext()));@b@ if (object instanceof Enumeration)@b@ return (!(((Enumeration)object).hasMoreElements()));@b@ if (object == null)@b@ throw new IllegalArgumentException("Unsupported object type: null");@b@ try@b@ {@b@ return (Array.getLength(object) == 0);@b@ } catch (IllegalArgumentException ex) {@b@ throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());@b@ }@b@ }@b@@b@ public static boolean isEmpty(Collection coll)@b@ {@b@ return ((coll == null) || (coll.isEmpty()));@b@ }@b@@b@ public static boolean isNotEmpty(Collection coll)@b@ {@b@ return (!(isEmpty(coll)));@b@ }@b@@b@ public static void reverseArray(Object[] array)@b@ {@b@ int i = 0;@b@ int j = array.length - 1;@b@@b@ while (j > i) {@b@ Object tmp = array[j];@b@ array[j] = array[i];@b@ array[i] = tmp;@b@ --j;@b@ ++i;@b@ }@b@ }@b@@b@ private static final int getFreq(Object obj, Map freqMap) {@b@ Integer count = (Integer)freqMap.get(obj);@b@ if (count != null)@b@ return count.intValue();@b@@b@ return 0;@b@ }@b@@b@ public static boolean isFull(Collection coll)@b@ {@b@ if (coll == null)@b@ throw new NullPointerException("The collection must not be null");@b@@b@ if (coll instanceof BoundedCollection)@b@ return ((BoundedCollection)coll).isFull();@b@ try@b@ {@b@ BoundedCollection bcoll = UnmodifiableBoundedCollection.decorateUsing(coll);@b@ return bcoll.isFull();@b@ } catch (IllegalArgumentException ex) {@b@ }@b@ return false;@b@ }@b@@b@ public static int maxSize(Collection coll)@b@ {@b@ if (coll == null)@b@ throw new NullPointerException("The collection must not be null");@b@@b@ if (coll instanceof BoundedCollection)@b@ return ((BoundedCollection)coll).maxSize();@b@ try@b@ {@b@ BoundedCollection bcoll = UnmodifiableBoundedCollection.decorateUsing(coll);@b@ return bcoll.maxSize();@b@ } catch (IllegalArgumentException ex) {@b@ }@b@ return -1;@b@ }@b@@b@ public static Collection retainAll(Collection collection, Collection retain)@b@ {@b@ return ListUtils.retainAll(collection, retain);@b@ }@b@@b@ public static Collection removeAll(Collection collection, Collection remove)@b@ {@b@ return ListUtils.retainAll(collection, remove);@b@ }@b@@b@ public static Collection synchronizedCollection(Collection collection)@b@ {@b@ return SynchronizedCollection.decorate(collection);@b@ }@b@@b@ public static Collection unmodifiableCollection(Collection collection)@b@ {@b@ return UnmodifiableCollection.decorate(collection);@b@ }@b@@b@ public static Collection predicatedCollection(Collection collection, Predicate predicate)@b@ {@b@ return PredicatedCollection.decorate(collection, predicate);@b@ }@b@@b@ public static Collection typedCollection(Collection collection, Class type)@b@ {@b@ return TypedCollection.decorate(collection, type);@b@ }@b@@b@ public static Collection transformedCollection(Collection collection, Transformer transformer)@b@ {@b@ return TransformedCollection.decorate(collection, transformer);@b@ }@b@}