Java Utililty Methods Collection Compare

List of utility methods to do Collection Compare

Description

The list of methods to do Collection Compare are organized into topic(s).

Method

intcompare(Collection c1, Collection c2)
Compares two collections of Comparable elements.
Iterator<? extends Comparable> itx1 = c1.iterator();
Iterator<? extends Comparable> itx2 = c2.iterator();
while (itx1.hasNext()) {
    if (!itx2.hasNext())
        return 1;
    Comparable v1 = itx1.next();
    Comparable v2 = itx2.next();
    int cmp = v1.compareTo(v2);
...
intcompare(Collection a, Collection b)
compare
int d = a.size() - b.size();
if (d != 0) {
    return d;
Iterator<T> ai = a.iterator();
Iterator<T> bi = b.iterator();
while (ai.hasNext()) {
    d = ai.next().compareTo(bi.next());
...
intcompare(Collection a, Collection b)
compare
int temp1 = a.size();
int temp2 = b.size();
if (temp1 > temp2)
    return 1;
if (temp1 < temp2)
    return -1;
Iterator<T> it1 = a.iterator();
Iterator<T> it2 = b.iterator();
...
intcompare(Collection lhs, Collection rhs)
compare
int cmp;
Iterator<T> lit = lhs.iterator();
Iterator<T> rit = rhs.iterator();
while (lit.hasNext() && rit.hasNext()) {
    cmp = lit.next().compareTo(rit.next());
    if (cmp != 0) {
        return cmp;
return lhs.size() - rhs.size();
intcompare(final Collection o1, final Collection o2)
compare
final Object[] a1 = o1.toArray(new Object[o1.size()]);
final Object[] a2 = o2.toArray(new Object[o2.size()]);
return compare(a1, a2);
booleancompare(final Collection c0, final Collection c1, final Comparator c)
Compares two Collections according to a comparator.
if ((c0 == null) || (c1 == null)) {
    throw new NullPointerException("The collections must not be null");
if (c == null) {
    throw new NullPointerException("The comparator must not be null");
if (c0.size() != c1.size()) {
    return false;
...
booleancompareAnyOrder(Collection c1, Collection c2)
compare Any Order
if (c1.size() != c2.size())
    return false;
for (Iterator<?> i1 = c1.iterator(); i1.hasNext();)
    if (!c2.contains(i1.next()))
        return false;
return true;
intcompareCollections(Collection a, Collection b)
Items are compared according to iteration order (when comparable).
Iterator it1 = a.iterator();
Iterator it2 = b.iterator();
while (it1.hasNext() && it2.hasNext()) {
    Object o1 = it1.next();
    Object o2 = it2.next();
    int r = tryCompare(o1, o2);
    if (r != 0)
        return r;
...
booleancompareCollections(Collection cola, Collection colb)
compare Collections
if (cola.size() != colb.size())
    return false;
Iterator ita = cola.iterator();
Iterator itb = colb.iterator();
while (ita.hasNext()) {
    Object itema = ita.next();
    Object itemb = itb.next();
    if (!itema.equals(itemb))
...
booleancompareCollections(Collection value1, Collection value2)
Compare two collections, handling null values as well.
return (value1 == null && value2 == null)
        || (value1 != null && value2 != null && value1.size() == value2.size() && value1.equals(value2));