Here you can find the source of equalsUnorderedCollections(Collection
Parameter | Description |
---|---|
a | a parameter |
b | a parameter |
public static <T> boolean equalsUnorderedCollections(Collection<T> a, Collection<T> b)
//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)); } }