Here you can find the source of difference(final Collection c1, final Collection c2)
public static Collection difference(final Collection c1, final Collection c2)
//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); } }