Java Collection Equal equal(Collection col1, Collection col2)

Here you can find the source of equal(Collection col1, Collection col2)

Description

Compares the given collections.

License

Open Source License

Parameter

Parameter Description
col1 collection 1
col2 collection 2

Return

comparison result

Declaration

public static boolean equal(Collection<?> col1, Collection<?> col2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006-2012//w  w w  . j  a  v  a 2 s  .co m
 * Software Technology Group, Dresden University of Technology
 * DevBoost GmbH, Berlin, Amtsgericht Charlottenburg, HRB 140026
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   Software Technology Group - TU Dresden, Germany;
 *   DevBoost GmbH - Berlin, Germany
 *      - initial API and implementation
 ******************************************************************************/

import java.util.Collection;

public class Main {
    /**
     * Compares the given collections.
     * 
     * @param col1 collection 1
     * @param col2 collection 2
     * @return comparison result
     */
    public static boolean equal(Collection<?> col1, Collection<?> col2) {
        int check = trivialCollectionEqual(col1, col2);
        switch (check) {
        case -1:
            return false;
        case 1:
            return true;
        default:
            //nothing
        }

        //TODO #1469: does not work if same element is contained twice
        boolean contains;
        for (Object eObj : col1) {
            contains = false;
            for (Object aObj : col2) {
                if (eObj.equals(aObj)) {
                    contains = true;
                    break;
                }
            }
            if (!contains) {
                return false;
            }
        }

        return true;
    }

    private static int trivialCollectionEqual(Collection<?> expected, Collection<?> actual) {
        if (expected == null && actual == null) {
            return 1;
        }
        if (expected == null && actual != null) {
            return -1;
        }
        if (actual == null && expected != null) {
            return -1;
        }
        if (expected == null || actual == null) {
            return -1;
        }
        if (expected.size() != actual.size()) {
            return -1;
        }
        if (expected.size() == 0 && actual.size() == 0) {
            return 1;
        }
        return 0;
    }
}

Related

  1. containsByEquals(Collection collection, Object obj)
  2. content_equality(Collection A, Collection B)
  3. contentEquals(Collection l1, Collection l2)
  4. contentEquals(Collection list1, Collection list2)
  5. equal(Collection i1, Collection i2)
  6. equalCollection(Collection c1, Collection c2)
  7. equalContent(Collection col1, Collection col2)
  8. equalContent(final Collection a, final Collection b)
  9. equals(Collection c1, Collection c2)