Java Collection Difference difference(final Collection c1, final Collection c2)

Here you can find the source of difference(final Collection c1, final Collection c2)

Description

difference

License

Open Source License

Declaration

public static Collection difference(final Collection c1,
            final Collection c2) 

Method Source Code

//package com.java2s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static Collection difference(final Collection c1,
            final Collection c2) {
        return union(leftDifference(c1, c2), rightDifference(c1, c2));
    }//from www .j a va  2 s.  c  o m

    public static Collection union(final Collection c1, final Collection c2) {
        final List list = new ArrayList();
        addAllUnique(list, c1);
        addAllUnique(list, c2);
        return list;
    }

    public static Collection leftDifference(final Collection c1,
            final Collection c2) {
        return rightDifference(c2, c1);
    }

    public static Collection rightDifference(final Collection c1,
            final Collection c2) {
        final List result = new ArrayList();
        final Object[] members_c1 = c1.toArray();
        for (int i = 0; i < members_c1.length; i++) {
            if (!c2.contains(members_c1[i])) {
                result.add(members_c1[i]);
            }
        }
        return result;
    }

    /**
     * add a collection of values to a collection.
     * 
     * @param collection
     *            the collection.
     * @param values
     *            the values.
     * @return boolean.
     * @since 0.1
     */
    public static boolean addAllUnique(final Collection collection,
            final Collection values) {
        boolean changed = false;
        for (final Iterator iter = values.iterator(); iter.hasNext();) {
            changed = addUnique(collection, iter.next());
        }
        return changed;
    }

    /**
     * add a unique value to a collection. If the value already exists, it will
     * return <code>false</code>.
     * 
     * @param collection
     *            the collection.
     * @param value
     *            the value.
     * @return boolean.
     * @since 0.1
     */
    public static boolean addUnique(final Collection collection,
            final Object value) {
        if (collection.contains(value)) {
            return false;
        }
        return collection.add(value);
    }
}

Related

  1. diff(Collection c1, Collection c2)
  2. diffColls(Collection oldColl, Collection newColl)
  3. difference(Collection set1, Collection set2)
  4. difference(Collection collection1, Collection result, Collection collection2)
  5. difference(Collection a, Collection b)
  6. difference(final Collection c1, final Collection c2)
  7. differentNull(Collection collection)
  8. getIntersectAndDiffs(Collection a, Collection b, Comparator comparator)
  9. getSymmetricDifference(final Set firstCollection, final Set secondCollection)