Checks if any of the elements in the first collection can be found in the second collection. - Java java.util

Java examples for java.util:Collection First Element

Description

Checks if any of the elements in the first collection can be found in the second collection.

Demo Code


//package com.java2s;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Collection findAnyOfThese = java.util.Arrays.asList("asdf",
                "java2s.com");
        Collection inThisCollection = java.util.Arrays.asList("asdf",
                "java2s.com");
        System.out// w w w.j av  a  2  s  .com
                .println(isAnyIncludedIn(findAnyOfThese, inThisCollection));
    }

    /**
     * Checks if any of the elements in the first collection can be found in the
     * second collection.
     *
     * @param findAnyOfThese which elements to look for.
     * @param inThisCollection where to look for them.
     * @param <E> type of the elements in question.
     * @return {@code true} if any of the elements in {@code findAnyOfThese} can
     *         be found in {@code inThisCollection}, {@code false} otherwise.
     */
    public static <E> boolean isAnyIncludedIn(Collection<E> findAnyOfThese,
            Collection<E> inThisCollection) {
        for (E findThisItem : findAnyOfThese) {
            if (inThisCollection.contains(findThisItem)) {
                return true;
            }
        }
        return false;
    }
}

Related Tutorials