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

Returns a such that a exists in c1 but not in c2.

License

Open Source License

Declaration

public static <T> List<T> difference(final Collection<T> c1, final Collection<T> c2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Collection;

import java.util.List;

public class Main {
    /**//from w w w  .  j a v a  2s .  c om
     * Returns a such that a exists in c1 but not in c2.
     * Always returns a new collection.
     */
    public static <T> List<T> difference(final Collection<T> c1, final Collection<T> c2) {
        if (c1 == null || c1.size() == 0) {
            return new ArrayList<>(0);
        }
        if (c2 == null || c2.size() == 0) {
            return new ArrayList<>(c1);
        }
        final List<T> difference = new ArrayList<>();
        for (final T current : c1) {
            if (!c2.contains(current)) {
                difference.add(current);
            }
        }
        return difference;
    }
}

Related

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