Java Collection Equal equals(Collection c1, Collection c2)

Here you can find the source of equals(Collection c1, Collection c2)

Description

Checks whether two collections are equal.

License

Apache License

Parameter

Parameter Description
c1 the first collection
c2 the second collection

Return

true if the collections are equal, else false.

Declaration

public static boolean equals(Collection<?> c1, Collection<?> c2) 

Method Source Code

//package com.java2s;
/* NOTICE: This file has been changed by Plutext Pty Ltd for use in docx4j.
 * The package name has been changed; there may also be other changes.
 * /*from w w  w  .  j av  a  2 s  . co m*/
 * This notice is included to meet the condition in clause 4(b) of the License. 
 */

import java.util.Collection;

public class Main {
    /**
     * <p>Checks whether two collections are equal. Two collections
     * C<sub>1</sub> and C<sub>2</sub> are equal, if the following conditions
     * are true:</p>
     *
     * <ul>
     *
     * <li><p>For each c<sub>1<em>i</em></sub> (element of C<sub>1</sub>) there
     * is a c<sub>2<em>j</em></sub> (element of C<sub>2</sub>), and
     * c<sub>1<em>i</em></sub> equals c<sub>2<em>j</em></sub>.</p></li>
     *
     * <li><p>For each c<sub>2<em>i</em></sub> (element of C<sub>2</sub>) there
     * is a c<sub>1<em>j</em></sub> (element of C<sub>1</sub>) and
     * c<sub>2<em>i</em></sub> equals c<sub>1<em>j</em></sub>.</p></li>
     *
     * </ul>
     *
     * @param c1 the first collection
     * @param c2 the second collection
     * @return <code>true</code> if the collections are equal, else
     * <code>false</code>.
     */
    public static boolean equals(Collection<?> c1, Collection<?> c2) {
        Object[] o1 = c1.toArray();
        Object[] o2 = c2.toArray();
        return internalEquals(o1, o2);
    }

    /**
     * <p>Compares to object arrays with regarding the objects' order. For
     * example, [1, 2, 3] and [2, 1, 3] are equal.</p>
     *
     * @param c1 The first object array.
     * @param c2 The second object array.
     * @return <code>true</code> if the object arrays are equal,
     * <code>false</code> if they are not.
     */
    public static boolean equals(Object[] c1, Object[] c2) {
        final Object[] o1 = c1.clone();
        final Object[] o2 = c2.clone();
        return internalEquals(o1, o2);
    }

    private static boolean internalEquals(Object[] o1, Object[] o2) {
        for (int i1 = 0; i1 < o1.length; i1++) {
            final Object obj1 = o1[i1];
            boolean matchFound = false;
            for (int i2 = 0; !matchFound && i2 < o1.length; i2++) {
                final Object obj2 = o2[i2];
                if (obj1.equals(obj2)) {
                    matchFound = true;
                    o2[i2] = null;
                }
            }
            if (!matchFound)
                return false;
        }
        return true;
    }
}

Related

  1. equal(Collection i1, Collection i2)
  2. equal(Collection col1, Collection col2)
  3. equalCollection(Collection c1, Collection c2)
  4. equalContent(Collection col1, Collection col2)
  5. equalContent(final Collection a, final Collection b)
  6. equals(Collection collection1, Collection collection2)
  7. equals(Collection collection1, Collection collection2)
  8. equals(Collection a, Collection b, boolean ordered)
  9. equals(final Collection c1, final Collection c2)