Java List Equal equal(List coll, List otherColl)

Here you can find the source of equal(List coll, List otherColl)

Description

equal

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static boolean equal(List coll, List otherColl) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006-2012//from   w  ww.  j av a 2 s . c  o 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.ArrayList;
import java.util.List;

public class Main {
    @SuppressWarnings("unchecked")
    public static boolean equal(List coll, List otherColl) {
        // trivial checks
        if (coll == null && otherColl == null)
            return true;

        // size checks
        if (coll == null && otherColl != null && otherColl.size() < 1)
            return true;
        if (coll != null && coll.size() < 1 && otherColl == null)
            return true;

        // trivial null check
        if (coll != null && coll.size() > 0 && otherColl == null)
            return false;
        if (coll == null && otherColl != null && otherColl.size() > 0)
            return false;

        // both empty
        if (coll.size() == 0 && otherColl.size() == 0)
            return true;

        List list = new ArrayList<Object>();
        list.addAll(coll);

        List otherList = new ArrayList<Object>();
        otherList.addAll(otherColl);

        if (list == null || list.size() < 1)
            return false;

        if (otherList != null) {
            boolean contains;
            boolean equal = true;
            Object obj;
            for (Object newObj : list) {
                contains = false;
                obj = null;
                for (Object oldObj : otherList) {
                    if (newObj.equals(oldObj)) {
                        contains = true;
                        obj = oldObj;
                        break;
                    }
                }
                equal = equal && contains;
                if (!equal)
                    break;
                otherList.remove(obj);
            }
            for (Object oldObj : otherList) {
                if (!equal)
                    break;
                contains = false;
                obj = null;
                for (Object newObj : list) {
                    if (oldObj.equals(newObj)) {
                        contains = true;
                        break;
                    }
                }
                equal = equal && contains;
            }
            if (equal)
                return true;
        }
        return false;
    }
}

Related

  1. areEqual(final List list1, final List list2)
  2. areEqual(List a, List b)
  3. areEqual(List list1, List list2)
  4. areEqualConsideringOrder(List listA, List listB)
  5. equal(List objects)
  6. equal(List v1, List v2)
  7. equalLists(List a, List b)
  8. equalLists(List aV1, List aV2)