Java Collection Contain containsAny(final Collection a, final Collection b)

Here you can find the source of containsAny(final Collection a, final Collection b)

Description

Returns true iff some element of a is also an element of b (or, equivalently, if some element of b is also an element of a).

License

Apache License

Parameter

Parameter Description
a a non-<code>null</code> Collection
b a non-<code>null</code> Collection

Return

true iff the intersection of a and b is non-empty

Declaration

public static boolean containsAny(final Collection a, final Collection b) 

Method Source Code

//package com.java2s;
/*/*from  w w  w . j  a v  a2 s  . c o  m*/
 * Copyright 1999-2004 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Returns <code>true</code> iff some element of <i>a</i> is also an element
     * of <i>b</i> (or, equivalently, if some element of <i>b</i> is also an
     * element of <i>a</i>). In other words, this method returns
     * <code>true</code> iff the {@link #intersection} of <i>a</i> and <i>b</i>
     * is not empty.
     * 
     * @since 2.1
     * @param a
     *            a non-<code>null</code> Collection
     * @param b
     *            a non-<code>null</code> Collection
     * @return <code>true</code> iff the intersection of <i>a</i> and <i>b</i>
     *         is non-empty
     * @see #intersection
     */
    public static boolean containsAny(final Collection a, final Collection b) {
        // TO DO: we may be able to optimize this by ensuring either a or b
        // is the larger of the two Collections, but I'm not sure which.
        for (Iterator iter = a.iterator(); iter.hasNext();) {
            if (b.contains(iter.next())) {
                return true;
            }
        }
        return false;
    }
}

Related

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