minus one collection from another collection - Java java.util

Java examples for java.util:Collection Operation

Description

minus one collection from another collection

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import java.util.Set;

public class Main {
    public static void main(String[] argv) {
        Collection c1 = java.util.Arrays.asList("asdf", "java2s.com");
        Collection s1 = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(minius(c1, s1));
    }/*from ww  w.  jav a  2  s .  c o m*/

    /**
     * 
     * @param c1
     * @param s1
     * @return c1 - s1
     * 
     */
    public static <T> List<T> minius(Collection<T> c1, Collection<T> s1) {

        if (c1 == null) {
            return Collections.emptyList();
        }

        if (s1 == null || s1.size() == 0) {
            return new ArrayList<T>(c1);
        }
        List<T> list = new ArrayList<T>();
        for (T t : c1) {
            if (!s1.contains(t)) {
                list.add(t);
            }
        }
        return list;
    }

    public static <T> List<T> minius(Collection<T> c1, Set<T> s1) {

        if (c1 == null) {
            return Collections.emptyList();
        }

        if (s1 == null || s1.size() == 0) {
            return new ArrayList<T>(c1);
        }
        List<T> list = new ArrayList<T>();
        for (T t : c1) {
            if (!s1.contains(t)) {
                list.add(t);
            }
        }
        return list;
    }
}

Related Tutorials