Java Collection Equal content_equality(Collection A, Collection B)

Here you can find the source of content_equality(Collection A, Collection B)

Description

this can be an expensive operation, intended for testing not operating code

License

Open Source License

Parameter

Parameter Description
N the shared type of the collections, supporting a working hashCode method
A a parameter
B a parameter

Return

whether A and B have the same elements and element frequencies.

Declaration

public static <N> boolean content_equality(Collection<N> A,
        Collection<N> B) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Main {
    /**/*from  www.  ja v a 2  s  .c om*/
     * this can be an expensive operation, intended for testing not operating code
     * @param <N> the shared type of the collections, supporting a working hashCode method
     * @param A 
     * @param B
     * @return whether A and B have the same elements and element frequencies.
     */
    public static <N> boolean content_equality(Collection<N> A,
            Collection<N> B) {
        if (A == B)
            return true;
        if (A == null || B == null)
            return false;
        if (A.size() != B.size())
            return false;

        //find the elements from both collections
        Set<N> elements = new HashSet<N>();
        elements.addAll(A);
        elements.addAll(B);

        //check the element frequencies are the same in both collections, any disparity = fail
        for (N element : elements) {
            int a_count = Collections.frequency(A, element);
            int b_count = Collections.frequency(B, element);
            if (a_count != b_count) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. areEqual(Collection collection1, Collection collection2)
  2. areEqual(Collection collection1, Collection collection2)
  3. compareTwoCollectionsEquality(Collection expectedAffectedFiles, Collection affectedFiles)
  4. containsAtleastOneEqualString(Collection left, String right)
  5. containsByEquals(Collection collection, Object obj)
  6. contentEquals(Collection l1, Collection l2)
  7. contentEquals(Collection list1, Collection list2)
  8. equal(Collection i1, Collection i2)
  9. equal(Collection col1, Collection col2)