Java Collection Equal equalsUnorderedCollections(Collection a, Collection b)

Here you can find the source of equalsUnorderedCollections(Collection a, Collection b)

Description

Compares the elements of two collections using Object.equals on the elements.

License

Open Source License

Parameter

Parameter Description
a a parameter
b a parameter

Return

True if a and b have the same number of elements and the elements are all equal.

Declaration

public static <T> boolean equalsUnorderedCollections(Collection<T> a, Collection<T> b) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Main {
    /**// w w  w .j av  a 2  s .c om
     * Compares the elements of two collections using Object.equals on the elements.  
     * Differences in the element order are not considered.
     * @param a
     * @param b
     * @return True if a and b have the same number of elements and the elements
     *         are all equal.
     */
    public static <T> boolean equalsUnorderedCollections(Collection<T> a, Collection<T> b) {

        // Do a quick check to see if a and b are the same object
        if (a == b) {
            return true;
        }

        // Ensure that both lists are the same size
        if (a.size() != b.size())
            return false;

        // Compare each element in turn and return false if any of them are different.
        List<T> _b = new ArrayList<T>(b);

        for (final T elemA : a) {

            boolean foundMatching = false;

            for (Iterator<T> iterB = _b.iterator(); iterB.hasNext();) {
                T elemB = iterB.next();
                if (equals(elemA, elemB)) {
                    iterB.remove();
                    foundMatching = true;
                    break;
                }
            }

            // We have walked past the end of the second list and still
            // cannot find a matching item.
            if (!foundMatching) {
                return false;
            }
        }

        // No mismatches were found, the list elements are equals
        return _b.isEmpty();
    }

    /**
     * Minor helper method that checks if 2 objects are either both null or equal using the equals()
     * method.  This makes long if statements (such as those found in equals() methods) easier to read. 
     * @param a
     * @param b
     * @return True if both a and b are null or if a.equals(b).  Otherwise false is returned.
     */
    public static boolean equals(Object a, Object b) {
        return (a == null ? b == null : a.equals(b));
    }
}

Related

  1. equalsOne(final T o1, final Collection others)
  2. equalsSet(Collection c1, Collection c2)
  3. equalsSet(Collection c1, Collection c2)
  4. equalsUnordered(Collection collection1, Collection collection2)
  5. equalsUnordered(Collection collection1, Collection collection2)
  6. equalTestData(Collection receivedDataSet)
  7. getContainedEquals(Collection a, T b)
  8. indexOfEquals(Collection collection, T element)
  9. isCollectionEqual(Collection c1, Collection c2)