Java Collection Difference difference(Collection collection1, Collection result, Collection collection2)

Here you can find the source of difference(Collection collection1, Collection result, Collection collection2)

Description

Stores the differences of two collections in a result collection.

License

Apache License

Parameter

Parameter Description
collection1 The first collection (master collection) that will be iterated and checked against duplicates in collection2.
result The collection to store the result
collection2 The second collection that will be searched for duplicates

Return

Returns the collection the user specified as the resulting collection

Declaration

public static <K> Collection<K> difference(Collection<K> collection1, Collection<K> result,
        Collection<K> collection2) 

Method Source Code

//package com.java2s;
/*/*  w w  w.  ja va 2 s  .co m*/
 * Copyright 2009 Thomas Bocek
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Stores the differences of two collections in a result collection. The result will contain items from collection1
     * without those items that are in collection2.
     * 
     * @param collection1
     *            The first collection (master collection) that will be iterated and checked against duplicates in
     *            collection2.
     * @param result
     *            The collection to store the result
     * @param collection2
     *            The second collection that will be searched for duplicates
     * @return Returns the collection the user specified as the resulting collection
     */
    public static <K> Collection<K> difference(Collection<K> collection1, Collection<K> result,
            Collection<K> collection2) {
        for (Iterator<K> iterator = collection1.iterator(); iterator.hasNext();) {
            K item = iterator.next();
            if (!collection2.contains(item)) {
                result.add(item);
            }
        }
        return result;
    }

    /**
     * Stores the differences of multiple collections in a result collection. The result will contain items from
     * collection1 without those items that are in collections2. The calling method might need to provide a
     * 
     * @SuppressWarnings("unchecked") since generics and arrays do not mix well.
     * @param collection1
     *            The first collection (master collection) that will be iterated and checked against duplicates in
     *            collection2.
     * @param result
     *            The collection to store the result
     * @param collection2
     *            The second collections that will be searched for duplicates
     * @return Returns the collection the user specified as the resulting collection
     */
    @SafeVarargs
    public static <K> Collection<K> difference(Collection<K> collection1, Collection<K> result,
            Collection<K>... collections2) {
        for (Iterator<K> iterator = collection1.iterator(); iterator.hasNext();) {
            K item = iterator.next();
            int size = collections2.length;
            boolean found = false;
            for (int i = 0; i < size; i++) {
                if (collections2[i].contains(item)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                result.add(item);
            }
        }
        return result;
    }
}

Related

  1. computeDifferenceIndices(Collection smallCollection, Collection bigCollection)
  2. diff(Collection c1, Collection c2)
  3. diffColls(Collection oldColl, Collection newColl)
  4. difference(Collection set1, Collection set2)
  5. difference(Collection a, Collection b)
  6. difference(final Collection c1, final Collection c2)
  7. difference(final Collection c1, final Collection c2)
  8. differentNull(Collection collection)