Description

Checks whether any element of a collection is present in another one.

License

Open Source License

Parameter

Parameter Description
A Key type
collection1 Container collection
collection2 Collection with elements to be checked

Return

true if any element in collection is present in container , and false otherwise. If container is empty, it will return false

Declaration

public static <A> boolean containsAny(Collection<A> collection1, Collection<A> collection2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors://from   www.j a v  a2  s.c o  m
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.*;

public class Main {
    /**
     * Checks whether any element of a collection is present in another one.
     *
     * @param <A> Key type
     * @param collection1 Container collection
     * @param collection2 Collection with elements to be checked
     * @return {@code true} if any element in {@code collection} is present in {@code container}, and {@code false} otherwise. If {@code container} is empty, it will return {@code false}
     */
    public static <A> boolean containsAny(Collection<A> collection1, Collection<A> collection2) {
        return !Collections.disjoint(collection1, collection2);
    }
}

Related

  1. containsAny(Collection collection1, Collection collection2)
  2. containsAny(Collection container, Collection contained)
  3. containsAny(Collection col1, Collection col2)
  4. containsAny(Collection source, Collection candidates)
  5. containsAny(Collection source, Object... candidates)
  6. containsAny(Collection c1, Collection c2)
  7. containsAny(Collection set, Iterable elements)
  8. containsAny(Collection c, String... toCheck)
  9. containsAny(Collection strs, Collection searchTerms)