Determine if the two collections contain exactly the same elements. - Java java.util

Java examples for java.util:Collection Contain

Description

Determine if the two collections contain exactly the same elements.

Demo Code


import java.util.Collection;
import java.util.Map;

public class Main{
    public static void main(String[] argv){
        Collection first = java.util.Arrays.asList("asdf","java2s.com");
        Collection second = java.util.Arrays.asList("asdf","java2s.com");
        System.out.println(equalsMany(first,second));
    }//www .  j  av  a 2 s.com
    /**
     * Determine if the two collections contain exactly the same elements.
     * <p>
     * Order doesn't matter if the first collection is a set.
     * @param first - the first collection, or NULL.
     * @param second - the second collection, or NULL.
     * @return TRUE if they are equal, FALSE otherwise.
     */
    public static <T> boolean equalsMany(Collection<T> first,
            Collection<T> second) {
        if (CollectionsUtil.isEmpty(first)
                ^ CollectionsUtil.isEmpty(second))
            return false;
        return first.containsAll(second);
    }
    /**
     * Determine if a given collection is empty or null.
     * @param collection - the collection to test.
     * @return TRUE if the collection is either empty or null, FALSE otherwise.
     */
    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }
    /**
     * Determine if a given map is empty or null.
     * @param map - the map to test.
     * @return TRUE if the map is either empty or null, FALSE otherwise.
     */
    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }
}

Related Tutorials