Get the difference of two collections : Collections Search « Collections « Java Tutorial

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

public class Utils {

  public static <T> Collection<T> diff(Collection<T> c1, Collection<T> c2) {
    if (c1 == null || c1.size() == 0 || c2 == null || c2.size() == 0) {
        return c1;
    Collection<T> difference = new ArrayList<T>();
    for (T item : c1) {
        if (!c2.contains(item)) {
    return difference;


9.41.Collections Search
9.41.1.Binary Searching
9.41.2.Check whether the given Collection contains the given element instance.
9.41.3.Binary search routines
9.41.4.Return the first element in 'candidates' that is contained in source
9.41.5.Find a value of the given type in the given Collection
9.41.6.Get the difference of two collections
9.41.7.A binary search implementation.