Java Collection Contain containsAny(Collection source, Collection candidates)

Here you can find the source of containsAny(Collection source, Collection candidates)

Description

Return true if any element in ' candidates ' is contained in ' source '; otherwise returns false .

License

Open Source License

Parameter

Parameter Description
source the source Collection
candidates the candidates to search for

Return

whether any of the candidates has been found

Declaration

public static boolean containsAny(Collection<?> source, Collection<?> candidates) 

Method Source Code


//package com.java2s;

import java.util.Collection;

import java.util.Map;

public class Main {
    /**/*from w w  w .ja  va  2  s. c  o m*/
     * Return {@code true} if any element in '{@code candidates}' is
     * contained in '{@code source}'; otherwise returns {@code false}.
     * @param source the source Collection
     * @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;
    }

    /**
     * Return {@code true} if the supplied Collection is {@code null}
     * or empty. Otherwise, return {@code false}.
     * @param collection the Collection to check
     * @return whether the given Collection is empty
     */
    public static boolean isEmpty(Collection<?> collection) {
        return (collection == null || collection.isEmpty());
    }

    public static boolean isEmpty(Object[] objectArray) {
        return (objectArray == null || objectArray.length == 0);
    }

    /**
     * Return {@code true} if the supplied Map is {@code null}
     * or empty. Otherwise, return {@code false}.
     * @param map the Map to check
     * @return whether the given Map is empty
     */
    public static boolean isEmpty(Map<?, ?> map) {
        return (map == null || map.isEmpty());
    }
}

Related

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