Java Collection Subtract subtract(Collection c1, Collection c2)

Here you can find the source of subtract(Collection c1, Collection c2)

Description

subtract

License

Apache License

Declaration

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection subtract(Collection c1, Collection c2) 

Method Source Code

//package com.java2s;
/*//w ww . ja  v a  2  s.  c  o m
 * Copyright 2014 the original author or authors.
 *
 * 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.
 *   
 * Create Date : 2014-9-29
 */

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

import java.util.Iterator;

import java.util.List;

public class Main {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Collection subtract(Collection c1, Collection c2) {
        if (isEmpty(c1) || isEmpty(c2))
            return c1;

        List list = newArrayList(c1);
        Iterator iterator = c2.iterator();
        Object element;
        while (iterator.hasNext()) {
            element = iterator.next();
            if (c2.contains(element))
                list.remove(element);
        }
        return list;
    }

    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.size() == 0;
    }

    public static <T> ArrayList<T> newArrayList() {
        return newArrayList(null);
    }

    public static <T> ArrayList<T> newArrayList(Collection<? extends T> collection) {
        return collection != null ? new ArrayList<T>(collection) : new ArrayList<T>();
    }

    public static int size(Collection<?> collection) {
        return collection != null ? collection.size() : 0;
    }
}

Related

  1. subCollection(Collection col, int amount)
  2. subCollection(final Collection collection, final int offset, final int limit)
  3. subMap(Map map, Collection keys)
  4. substractBackground( final Map> concentrationToSampleReadings, final Map> concentrationToBackgroundReadings)
  5. subtract(Collection a, Collection b)
  6. subtract(Collection a, Collection b)
  7. subtract(Collection c1, Collection c2)
  8. subtract(Collection l1, Collection l2)
  9. subtract(Collection l1, Collection l2)