Example usage for java.util Collection contains

List of usage examples for java.util Collection contains

Introduction

In this page you can find the example usage for java.util Collection contains.

Prototype

boolean contains(Object o);

Source Link

Document

Returns true if this collection contains the specified element.

Usage

From source file:Main.java

public static <T> Set<T> intersect(final Collection<? extends T> c1, final Collection<? extends T> c2) {
    final Set<T> set = new HashSet<T>();
    for (T t : c1) {
        if (c2.contains(t))
            set.add(t);//from  www  . j  a v  a  2  s  .  co m
    }
    return set;
}

From source file:Main.java

/**
 * Compares two collections returning the number of elements they have in common
 * //from   w  w  w.j  av a2  s. co m
 * @param <T>
 * @param one
 * @param two
 * @return
 */
public static <T> int collectionCompare(Collection<T> one, Collection<T> two) {
    int count = 0;

    Iterator<T> e = one.iterator();
    while (e.hasNext()) {
        if (two.contains(e.next()))
            ++count;
    }

    return count;
}

From source file:Main.java

public static boolean isSubsetStringOf(Collection<String> subset, Collection<String> superset) {
    if (subset != null && superset != null) {
        for (String string : subset) {
            if (!superset.contains(string)) {
                return false;
            }/*from ww  w  .jav  a 2s. com*/
        }
    }
    return true;
}

From source file:Main.java

public static <T> List<T> difference(Collection<T> list, Collection<T> otherList) {
    ArrayList<T> difference = new ArrayList<T>();
    for (T object : list) {
        boolean found = otherList.contains(object);
        if (!found) {
            difference.add(object);//  w ww.  jav a  2 s. c  om
        }
    }

    return difference;
}

From source file:Main.java

/** @return whether given coll contains all elements of other, using a naive/basic algorithm. */
public static boolean containsAll(Collection<?> coll, Collection<?> other) {
    Iterator<?> citer = other.iterator();
    while (citer.hasNext())
        if (!coll.contains(citer.next()))
            return false;
    return true;/*w ww.  j a v a2s  .c  o  m*/
}

From source file:Main.java

public static <T> double compuateJaccard(Collection<T> one, Collection<T> two) {
    int intersection = 0;
    int union = 0;
    for (T item : one) {
        if (two.contains(item)) {
            intersection++;/*from  w  w w  .  j  a v a  2  s .com*/
        }
        union++;
    }
    for (T item : two) {
        if (!one.contains(item)) {
            union++;
        }
    }
    return (double) intersection / (double) union;
}

From source file:Main.java

/**
 * Inserts into one collection all elements of another collection not
 * contained in that collection./*from w  ww.j  a v  a 2 s  .c  om*/
 * <p>
 * Uses {@code Collection#contains(java.lang.Object)} to compare elements.
 *
 * @param <T>    the collection's element type
 * @param src    source collection to get elements from
 * @param target target collection to put elements into
 */
public static <T> void addNotContainedElements(Collection<? extends T> src, Collection<? super T> target) {
    if (src == target) {
        return;
    }

    for (T t : src) {
        if (!target.contains(t)) {
            target.add(t);
        }
    }
}

From source file:Main.java

final public static <T> Collection<T> nonMatched(Collection<T> tested, Collection<T> sources) {
    Collection<T> nonMatched = new ArrayList<T>();
    for (T source : sources) {
        if (!tested.contains(source)) {
            nonMatched.add(source);/* w w  w. j  av  a 2s . co m*/
        }
    }
    return nonMatched;
}

From source file:Main.java

public static boolean hasAtLeastOneNotNullElement(Collection<?> collection) {
    if (collection == null)
        return false;
    if (collection.isEmpty())
        return false;
    if (collection.contains(null))
        return collection.size() > 1;
    return collection.size() > 0;
}

From source file:Main.java

public static <T> List intersection(Collection<T> a, Collection<T> b) {
    List<T> intersection = new ArrayList<T>();
    if (a != null && b != null) {
        for (T o : a) {
            if (b.contains(o))
                intersection.add(o);/* www . j av a  2s  .  c om*/
        }
    }
    return intersection;
}