Java Collection Equal isEqual(Collection a, Collection b)

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

Description

is Equal

License

Open Source License

Parameter

Parameter Description
a The first collection to compare
b The second collection to compare
T The generic type of the collections to compare

Return

true if a == b or a and b contain equal elements in the same order

Declaration

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

Method Source Code

//package com.java2s;
/**/*from ww w.  j  av a2 s.c  om*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/.
 */

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**
     * @param a   The first value to compare
     * @param b   The second value to compare
     * @param <T> The type of the values
     * @return true if a == b or a equals b
     */
    public static <T> boolean isEqual(T a, T b) {
        return a == b || (a != null) && a.equals(b);
    }

    /**
     * @param a   The first collection to compare
     * @param b   The second collection to compare
     * @param <T> The generic type of the collections to compare
     * @return true if a == b or a and b contain equal elements in the same order
     */
    public static <T> boolean isEqual(Collection<T> a, Collection<T> b) {
        if (a == null) {
            return b == null;
        }
        if (b == null) {
            return false;
        }
        if (a.size() != b.size()) {
            return false;
        }
        for (Iterator<T> itA = a.iterator(), itB = b.iterator(); itA.hasNext();) {
            if (!isEqual(itA.next(), itB.next())) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. isCollectionsEqual(Collection f1, Collection f2)
  2. isContentEqual(final Collection collectionA, final Collection collectionB)
  3. isEqual(Collection left, Collection right)
  4. isEqual(Collection one, Collection two)
  5. isEqual(Collection coll, Comparator comp)
  6. isEqualBasicCol(Collection col1, Collection col2)
  7. isEqualCollection(final Collection a, final Collection b)
  8. isEqualCollection(final Collection a, final Collection b)
  9. isEqualDeep(Collection a, Collection b)