Returns true iff some element of a is also an element of b, between two Collection - Android java.util

Android examples for java.util:Collection Filter

Description

Returns true iff some element of a is also an element of b, between two Collection

Demo Code

/*//from   w ww  . ja 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.
 */
//package com.java2s;

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 Tutorials