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

/**
 * Returns <code>true</code> iff at least one element is in both collections.
 * <p>/*from w  ww  .j a  va 2 s  .c  om*/
 * 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
 * @since 2.1
 * @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;
}

From source file:Main.java

public static <E> void transferElementsToHead(List<E> source, List<E> target,
        Collection<E> elementsToTransfer) {
    // iterate the source in reverse order so that the order in the target is correct
    for (int i = source.size() - 1; i >= 0; i--) {
        E sourceTag = source.get(i);/*from   w  ww  . j  ava  2 s. com*/
        if (elementsToTransfer.contains(sourceTag)) {
            target.add(0, sourceTag);
        }
    }
}

From source file:Main.java

/**
 * Filters all objects in the list against a specified collection. Either removes all items that
 * are found in specified collection or removes all items that are not found in the collection.
 * Uses {@link Iterator#remove()} to remove items from the target list.
 * /*from   w w w .ja v  a 2s.  co  m*/
 * @param items collection of items acting as a filter
 * @param iter iterator over the target collection, from which items will be removed
 * @param remove true if <em>removing</em> all items found in the collection or false if
 *           <em>retaining</em> them
 * @return true if the list was modified and something was removed
 */
// TODO: add javadoc about using this to implement Collection.removeAll/retainAll
public static boolean filter(Collection<?> items, Iterator<?> iter, boolean remove) {
    boolean modified = false;
    while (iter.hasNext()) {
        if (items.contains(iter.next()) == remove) {
            iter.remove();
            modified = true;
        }
    }
    return modified;
}

From source file:Main.java

public static Collection<Object> filterCollection(Collection<Object> src, Collection<Object> dest) {

    if (isEmpty(dest))
        return src;
    Iterator<Object> iterator = src.iterator();
    while (iterator.hasNext()) {

        Object next = iterator.next();
        if (dest.contains(next)) {

            iterator.remove();/* w w  w .  jav a2s. com*/
        }
    }

    return src;
}

From source file:de.dhke.projects.cutil.collections.MultiMapUtil.java

public static <V> Collection<V> getTransitive(final MultiMap<V, V> multiMap, final V key,
        final Collection<V> targetSet) {
    Collection<V> keyValues = multiMap.get(key);
    if (keyValues != null) {
        for (V keyValue : keyValues) {
            if (!targetSet.contains(keyValue)) {
                targetSet.add(keyValue);
                targetSet.addAll(getTransitive(multiMap, keyValue, targetSet));
            }/*from   w w w.ja  v a2  s.  co m*/
        }
    }
    return targetSet;
}

From source file:de.dhke.projects.cutil.collections.CollectionUtil.java

/**
 * Return {@literal true}, if {@literal sup} contains at least one item from {@literal parts}.
 *
 * @param <E>//from  ww w  .  j ava 2s . c  o  m
 * @param sup
 * @param parts
 * <p/>
 * @return {@literal true} if at least one item of {@literal parts} was found in {@literal sup}.
 */
public static <E> boolean containsOne(final Collection<E> sup, final Collection<? extends E> parts) {
    for (E part : parts) {
        if (sup.contains(part))
            return true;
    }

    return false;
}

From source file:Main.java

/**
 * Returns <code>true</code> iff at least one element is in both collections15.
 * <p/>/*from   w w  w.  jav a  2s  .  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 collections15 is non-empty
 * @see #intersection
 * @since 2.1
 */
public static <E> boolean containsAny(final Collection<? extends E> coll1,
        final Collection<? extends E> 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;
}

From source file:Main.java

static String findSettableValue(Collection<String> supportedValues, String... desiredValues) {
    String result = null;/*ww w . j  a  va 2 s .  c o  m*/
    if (supportedValues != null) {
        for (String desiredValue : desiredValues) {
            if (supportedValues.contains(desiredValue)) {
                result = desiredValue;
                break;
            }
        }
    }
    return result;
}

From source file:Main.java

/**
 * Return <code>true</code> if any element in '<code>candidates</code>' is
 * contained in '<code>source</code>'; otherwise returns <code>false</code>.
 * @param source the source Collection//from   w w  w . j  a va 2 s .  c  o m
 * @param candidates the candidates to search for
 * @return whether any of the candidates has been found
 */
public static boolean containsAny(Collection source, Collection candidates) {
    if (isEmpty(source) || isEmpty(candidates)) {
        return false;
    }
    for (Object candidate : candidates) {
        if (source.contains(candidate)) {
            return true;
        }
    }
    return false;
}

From source file:com.personal.tools.Collections3.java

/**
 * abList.//w w w . jav  a  2  s  . c o m
 */
public static <T> List<T> intersection(Collection<T> a, Collection<T> b) {
    List<T> list = new ArrayList<>();

    for (T element : a) {
        if (b.contains(element)) {
            list.add(element);
        }
    }
    return list;
}