Java Collection Contain containsAll(Collection collectionToCheck, Collection items)

Here you can find the source of containsAll(Collection collectionToCheck, Collection items)

Description

contains All

License

Open Source License

Declaration

public static boolean containsAll(Collection<?> collectionToCheck, Collection<?> items) 

Method Source Code


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

import java.util.Iterator;
import java.util.List;

import java.util.Set;

public class Main {
    public static boolean containsAll(Collection<?> collectionToCheck, Collection<?> items) {
        for (Object o : items) {
            if (!collectionToCheck.contains(o)) {
                return false;
            }//w  w  w .j  av a 2 s .c  om
        }
        return true;
    }

    public static boolean contains(Iterator<?> iteratorToCheck, Object item) {
        while (iteratorToCheck.hasNext()) {
            Object o = iteratorToCheck.next();
            if (o == null ? item == null : o.equals(item)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Implements <em>equals</em> per the contract defined by {@link List#equals(Object)}.
     * 
     * @param list the list
     * @param o an object to compare to the list
     * @return true if {@code o} equals {@code list}
     */
    public static boolean equals(List<?> list, Object o) {
        if (!(o instanceof List)) {
            return false;
        }
        if (list == o) {
            return true;
        }
        List<?> l = (List<?>) o;
        if (l.size() != list.size()) {
            return false;
        }
        return listContentsEquals(list, l);
    }

    /**
     * Implements <em>equals</em> per the contract defined by {@link Set#equals(Object)}.
     * 
     * @param set the set
     * @param o an object to compare to the list
     * @return true if {@code o} equals {@code list}
     */
    public static boolean equals(Set<?> set, Object o) {
        if (!(o instanceof Set)) {
            return false;
        }
        if (set == o) {
            return true;
        }
        Set<?> other = (Set<?>) o;
        if (set.size() != other.size()) {
            return false;
        }
        // The spec for interface Set says a set should never contain itself, but most implementations
        // do not explicitly block this. So the paranoid code below handles this case.
        boolean containsItself = false;
        for (Object element : set) {
            if (element == set || element == other) {
                // don't test using contains(...) since that could cause infinite recursion
                containsItself = true;
            } else {
                if (!other.contains(element)) {
                    return false;
                }
            }
        }
        if (containsItself) {
            // safely check that other also contains itself
            for (Object element : other) {
                if (element == set || element == other) {
                    return true;
                }
            }
            return false;
        }
        return true;
    }

    private static boolean listContentsEquals(Iterable<?> l1, Iterable<?> l2) {
        Iterator<?> iter1 = l1.iterator();
        Iterator<?> iter2 = l2.iterator();
        while (iter1.hasNext()) {
            if (!iter2.hasNext()) {
                return false;
            }
            Object e1 = iter1.next();
            Object e2 = iter2.next();
            if ((e1 == l1 && (e2 == l1 || e2 == l2)) || (e1 == l2 && (e2 == l1 || e2 == l2))) {
                // handle case where list contains itself - don't overflow stack
                continue;
            }
            if (e1 == null ? e2 != null : !e1.equals(e2)) {
                return false;
            }
        }
        if (iter2.hasNext()) {
            return false;
        }
        return true;
    }
}

Related

  1. contains(final Collection mimeTypes, final MimeType mimeType)
  2. contains(final Collection c, final T o)
  3. contains(Object o, Collection c)
  4. containsAll(Collection c, Iterable subset)
  5. containsAll(Collection coll, Collection other)
  6. containsAll(final Collection c, final Object... array)
  7. containsAll(final Collection coll1, final Collection coll2)
  8. containSameElements(Collection> sets)
  9. containSameItems(Collection c1, Collection c2)