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> List<T> intersection(Collection<T> a, Collection<T> b) {
    List<T> list = new ArrayList<T>();

    for (T element : a) {
        if (b.contains(element)) {
            list.add(element);//  w  w  w .j  av  a  2  s  .c  o m
        }
    }
    return list;
}

From source file:Main.java

/**
 * @return true if collection is not null and contains the items
 *//*from  w w  w  . java  2s  .co  m*/
public static boolean contains(final Collection<?> collection, final Object item) {
    return collection != null && collection.contains(item) ? true : false;
}

From source file:Main.java

/**
 * Returns all objects in list1 that are not in list2
 *
 * @param <T> Type of items in the collection
 * @param list1 First collection/*from  w ww. j  a v  a  2s  . com*/
 * @param list2 Second collection
 * @return The collection difference list1 - list2
 */
public static <T> Collection<T> diff(Collection<T> list1, Collection<T> list2) {
    Collection<T> diff = new ArrayList<T>();
    for (T t : list1) {
        if (!list2.contains(t)) {
            diff.add(t);
        }
    }
    return diff;
}

From source file:Main.java

public static <T> boolean containsAny(Collection<T> collection, Collection<T> referenceElements) {
    for (T referenceElement : referenceElements) {
        if (collection.contains(referenceElement)) {
            return true;
        }/*from   w  w  w.  j  a  va  2s  . co  m*/
    }
    return false;
}

From source file:com.vmware.identity.rest.core.server.test.authorization.token.builder.SAMLTokenBuilderTest.java

private static void assertCollectionEquals(String message, Collection<?> expected, Collection<?> actual) {
    for (Object e : expected) {
        if (!actual.contains(e)) {
            fail(message);//from   w w  w  . j a v a  2  s .  c  o m
        }
    }
}

From source file:Main.java

public static <T extends Object> boolean hasComplement(Collection<T> c1, Collection<T> c2) {
    if (c1 == null || c2 == null)
        return false;
    for (T t1 : c1) {
        if (!c2.contains(t1))
            return true;
    }/*w w  w  .ja v a2s .c  om*/
    for (T t2 : c2) {
        if (!c1.contains(t2))
            return true;
    }
    return false;
}

From source file:Main.java

/**
 * Answers true if a predicate is true for at least one element of a
 * collection./*from   w  w  w  .  ja  v  a2 s .c o  m*/
 *
 * @param <T>
 * @param collection - the collection to get the input from, may be null
 * @param value      - value to check
 * @return true if a collection contains specified value.
 */
public static <T> boolean contains(Collection<T> collection, T value) {
    return collection != null && collection.size() > 0 && collection.contains(value);
}

From source file:Main.java

/**
 * Answers true if a predicate is true for at least one element of a
 * collection./*w ww  .  j  a  v a2 s .  c o m*/
 * 
 * @param <T>
 * @param collection
 *            - the collection to get the input from, may be null
 * @param value
 *            - value to check
 * @return true if a collection contains specified value.
 */
public static <T> boolean contains(Collection<T> collection, T value) {
    return collection != null && collection.size() > 0 ? collection.contains(value) : false;
}

From source file:Main.java

public static <E> Collection<E> asymmetricDifference(E[] c1, Collection<E> c2) {
    final Set<E> result = new HashSet<E>();
    for (final E elem : c1) {
        if (!c2.contains(elem)) {
            result.add(elem);/*from  www. j  a v  a2  s  .  c o  m*/
        }
    }
    return result;
}

From source file:Main.java

public static <T> int addAllThatDontExist(final Collection<T> source, final Collection<T> destination) {
    int count = 0;
    for (final T sourceItem : source) {
        if (!destination.contains(sourceItem)) {
            destination.add(sourceItem);
            ++count;/*  w  w w .  j av a2s .  com*/
        }
    }

    return count;
}