Java Collection Subtract sub(Collection s, T item)

Here you can find the source of sub(Collection s, T item)

Description

sub

License

Open Source License

Declaration

public static <T> Collection<T> sub(Collection<T> s, T item) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    public static <T> Collection<T> sub(Collection<T> s, T item) {
        Set<T> result = new HashSet<T>();
        for (T t : s)
            if (t != item)
                result.add(t);/*w  w w .  j  a v  a2 s  .c o  m*/
        return result;
    }

    public static <T> List<T> sub(List<T> s, T item) {
        List<T> result = new ArrayList<T>(s);
        result.remove(item);
        return result;
    }

    public static <T> HashSet<T> sub(Set<T> s, T item) {
        HashSet<T> result = new HashSet<T>();
        for (T t : s)
            if (t != item)
                result.add(t);
        return result;
    }

    public static <K, V> HashMap<K, V> sub(Map<K, V> s, K key) {
        HashMap<K, V> result = new HashMap<K, V>(s);
        result.remove(key);
        return result;
    }

    public static <T> ArrayList<T> add(Collection<T> a, Collection<T> b) {
        ArrayList<T> result = (ArrayList<T>) list();
        result.addAll(a);
        result.addAll(b);
        return result;
    }

    public static <T> HashSet<T> add(Set<T> a, Set<T> b) {
        HashSet<T> result = (HashSet<T>) set();
        result.addAll(a);
        result.addAll(b);
        return result;
    }

    public static <T> ArrayList<T> list(T... tt) {
        return new ArrayList<T>(Arrays.asList(tt));
    }

    public static List list() {
        return new ArrayList();
    }

    public static <T> Set<T> set(T... tt) {
        return new HashSet<T>(Arrays.asList(tt));
    }
}

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)