Java Collection Contain containsAny(final Collection coll1, final Collection coll2)

Here you can find the source of containsAny(final Collection coll1, final Collection coll2)

Description

Returns true iff at least one element is in both collections.

License

Open Source License

Parameter

Parameter Description
coll1 the first collection, must not be null
coll2 the first collection, must not be null

Return

true iff the intersection of the collections is non-empty

Declaration

public static boolean containsAny(final Collection coll1, final Collection coll2) 

Method Source Code


//package com.java2s;

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**//from  w ww .  j  a v a  2  s .  c  o  m
     * Returns <code>true</code> iff at least one element is in both collections.
     * <p>
     * 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
     */
    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;
    }
}

Related

  1. containsAny(Collection collection, Collection toCheck)
  2. containsAny(Collection list, Collection values)
  3. containsAny(Collection src, Collection containsAny)
  4. containsAny(Collection firstCollection, Collection secondCollection)
  5. containsAny(final Collection a, final Collection b)
  6. containsAny(final Collection collection, final Object... items)
  7. containsAny(final Collection collection1, final Collection collection2)
  8. containsAny(final Collection a, final Collection b)
  9. containsAnyIgnoreCase(String string, Collection searchStrings)