Java Collection Equal isEqual(Collection one, Collection two)

Here you can find the source of isEqual(Collection one, Collection two)

Description

Calculates the equality of two collections disregarding the underlying implementation.

License

Apache License

Parameter

Parameter Description
one a Collection
two a Collection

Return

true if the collections are equal

Declaration

public static boolean isEqual(Collection one, Collection two) 

Method Source Code

//package com.java2s;
/*/*from  www .j av a  2  s .  c o m*/
 * Copyright 2014 Johns Hopkins University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;

public class Main {
    /**
     * Calculates the equality of two collections disregarding the underlying implementation.  Specifically this allows
     * (for example) a {@code Collection} implemented using an {@code ArrayList} and a {@code Collection} implemented
     * using a {@code HashSet} to be compared for equality.
     * <p>
     * This method uses {@link Collection#containsAll(java.util.Collection)} to determine equality of the supplied
     * collections.
     *
     * @param one a Collection
     * @param two a Collection
     * @return true if the collections are equal
     */
    public static boolean isEqual(Collection one, Collection two) {
        if (one == null && two != null) {
            return false;
        }

        if (one != null && two == null) {
            return false;
        }

        if (one == two) {
            return true;
        }

        return one.containsAll(two) && two.containsAll(one);
    }
}

Related

  1. indexOfEquals(Collection collection, T element)
  2. isCollectionEqual(Collection c1, Collection c2)
  3. isCollectionsEqual(Collection f1, Collection f2)
  4. isContentEqual(final Collection collectionA, final Collection collectionB)
  5. isEqual(Collection left, Collection right)
  6. isEqual(Collection coll, Comparator comp)
  7. isEqual(Collection a, Collection b)
  8. isEqualBasicCol(Collection col1, Collection col2)
  9. isEqualCollection(final Collection a, final Collection b)