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

/**
 * Adds all objects into the specified list.
 *
 * @param collection list to fill//from   www .j ava 2 s  .  c  om
 * @param objects    objects
 * @param <T>        objects type
 * @return true if list changed as the result of this operation, false otherwise
 */
public static <T> boolean addAll(final Collection<T> collection, final Collection<T> objects) {
    boolean result = false;
    for (final T object : objects) {
        if (!collection.contains(object)) {
            result |= collection.add(object);
        }
    }
    return result;
}

From source file:Main.java

private static void _addSuperTypes(Class<?> cls, Class<?> endBefore, Collection<Class<?>> result,
        boolean addClassItself) {
    if (cls == endBefore || cls == null || cls == Object.class) {
        return;//from  w  w w .j  av  a2 s  .c  o  m
    }
    if (addClassItself) {
        if (result.contains(cls)) { // already added, no need to check supers
            return;
        }
        result.add(cls);
    }
    for (Class<?> intCls : cls.getInterfaces()) {
        _addSuperTypes(intCls, endBefore, result, true);
    }
    _addSuperTypes(cls.getSuperclass(), endBefore, result, true);
}

From source file:Main.java

public static <T> Set<T> getIntersectionValues(Collection<? extends T> colection1,
        Collection<? extends T> colection2) {

    Set<T> ret = new HashSet<T>();
    if (colection1 == null || colection2 == null)
        return ret;

    for (T value : colection1) {
        if (colection2.contains(value))
            ret.add(value);/*  w  w  w  .  j  a  v a 2  s . c  om*/
    }
    return ret;
}

From source file:Main.java

public static <T> boolean containsAny(Collection<T> collection, Collection<T> query) {
    if (collection == null || collection.isEmpty() || query == null || query.isEmpty())
        return false;
    for (T val : query) {
        if (collection.contains(val))
            return true;
    }//from   w  ww .  j  av a 2s  .  c  o  m
    return false;
}

From source file:Main.java

/**
 * added by liyong//ww w.  j a va 2 s  . c  om
 */
public static <T> Collection<T> same(Collection<T> c1, Collection<T> c2) {
    if (isEmpty(c1) || isEmpty(c2))
        return new ArrayList<T>(0);

    List<T> result = new ArrayList<T>();
    for (T item : c2) {
        if (c1.contains(item)) {
            result.add(item);
        }
    }
    return result;
}

From source file:Main.java

/**
 * Return the first element in '<code>candidates</code>' that is contained in
 * '<code>source</code>'. If no element in '<code>candidates</code>' is present in
 * '<code>source</code>' returns <code>null</code>. Iteration order is
 * {@link java.util.Collection} implementation specific.
 *
 * @param source     the source Collection
 * @param candidates the candidates to search for
 * @return the first present object, or <code>null</code> if not found
 *//*from  w w  w  .  ja  v  a 2 s .c o  m*/
public static Object findFirstMatch(Collection source, Collection candidates) {
    if (isEmpty(source) || isEmpty(candidates)) {
        return null;
    }
    for (Object candidate : candidates) {
        if (source.contains(candidate)) {
            return candidate;
        }
    }
    return null;
}

From source file:Utils.java

public static <T> Collection<T> diff(Collection<T> c1, Collection<T> c2) {
    if (c1 == null || c1.size() == 0 || c2 == null || c2.size() == 0) {
        return c1;
    }/*from   www  .  j  a va 2  s .  c  o m*/
    Collection<T> difference = new ArrayList<T>();
    for (T item : c1) {
        if (!c2.contains(item)) {
            difference.add(item);
        }
    }
    return difference;
}

From source file:Main.java

private static void extractAddedEntries(final Collection<String> oldCollection,
        final Collection<String> updatedCollection, final Collection<String> addedEntriesCollection) {
    for (final String entry : updatedCollection) {
        if (!oldCollection.contains(entry)) {
            addedEntriesCollection.add(entry);
        }// w  w w  .ja v  a  2  s.com
    }
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static boolean allNotIn(Collection source, Collection target) {
    if (target == null || target.size() == 0) {
        return true;
    }//  ww w .  ja v  a 2s . c om
    for (Iterator it = source.iterator(); it.hasNext();) {
        Object candidate = it.next();
        if (target.contains(candidate)) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Returns <code>true</code> iff at least one element is in both collections.
 * <p/>//from  w ww. ja  v a  2  s .  c  o m
 * In other words, this method returns <code>true</code> iff the
 * {@link #intersection} of <i>coll1</i> and <i>coll2</i> is not empty.
 *
 * @param coll1 the first collection, must not be null
 * @param coll2 the first collection, must not be null
 * @return <code>true</code> iff the intersection of the collections is non-empty
 * @see #intersection
 */
public static boolean containsAny(final Collection coll1, final Collection coll2) {
    if (coll1.size() < coll2.size()) {
        for (Iterator it = coll1.iterator(); it.hasNext();) {
            if (coll2.contains(it.next()))
                return true;
        }
    } else {
        for (Iterator it = coll2.iterator(); it.hasNext();) {
            if (coll1.contains(it.next()))
                return true;
        }
    }
    return false;
}