Can we have Collection.containsAny(Collection<?>)

Lukas Eder lukas.eder at gmail.com
Wed Mar 20 01:33:51 PDT 2013


Hello,

We already have a method to check if *all* elements of Collection A
are contained in Collection B:

    boolean all = B.containsAll(A);

I often find myself trying to see if *any* element in Collection A is
contained in Collection B. I think this would be a low-hanging fruit
to add to the Collection interface as a default method:

    boolean any = B.containsAny(A);

With

    default boolean containsAny(Collection<?> c) {
        for (Object e : c)
            if (contains(e))
                return true;

        return false;
    }

Also, are there any plans of adding default implementations for other
Collection methods, e.g.

    // Promoted from AbstractCollection
    default boolean containsAll(Collection<?> c) {
        for (Object e : c)
            if (!contains(e))
                return false;

        return true;
    }

Or is there some subtle backwards-compatibility issue that I'm missing?

Cheers
Lukas


More information about the lambda-libs-spec-observers mailing list