Returns all items in collection 1 that are not in collection 2 - Java java.util

Java examples for java.util:Collection Compare

Description

Returns all items in collection 1 that are not in collection 2

Demo Code


//package com.book2s;
import java.util.ArrayList;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Collection collection1 = java.util.Arrays.asList("asdf",
                "book2s.com");
        Collection collection2 = java.util.Arrays.asList("asdf",
                "book2s.com");
        System.out.println(subtractCollections(collection1, collection2));
    }//from  w  w  w.  j av  a 2  s .c  o  m

    /**
     * Returns all items in collection1 that are not in collection2
     */
    public static <T> Collection<T> subtractCollections(
            Collection<? extends T> collection1,
            Collection<? extends T> collection2) {
        Collection<T> result = new ArrayList<T>(collection1);
        result.removeAll(collection2);
        return result;
    }
}

Related Tutorials