Java List Equal isEqual(Collection listA, Collection listB)

Here you can find the source of isEqual(Collection listA, Collection listB)

Description

Return true if same objects exist in listA and listB

License

Open Source License

Declaration

public static <T> boolean isEqual(Collection<T> listA, Collection<T> listB) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing./*from  www . ja va2s  .co m*/
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

public class Main {
    /**
     * Return true if same objects exist in listA and listB
     */
    public static <T> boolean isEqual(Collection<T> listA, Collection<T> listB) {
        if (listA.size() != listB.size()) {
            return false;
        }
        if (listA.size() != setIntersection(listA, listB).size()) {
            return false;
        }
        return true;
    }

    /**
     * @return The intersection of two sets A and B is the set of elements common to A and B
     */
    public static <T> List<T> setIntersection(Collection<T> listA, Collection<T> listB) {
        ArrayList<T> intersection = new ArrayList<T>(listA.size());

        for (T obj : listA) {
            if (listB.contains(obj)) {
                intersection.add(obj);
            }
        }
        return intersection;
    }
}

Related

  1. equals(List lhs, List rhs, Comparator comparator)
  2. equals(List list1, List list2)
  3. equalsAny(String toMatch, List matchesAny)
  4. equalsBasedOnEntryIdentity(final List a, final List b)
  5. equalShallow(List list0, List list1)
  6. isEqual(List list1, List list2)
  7. isEqual(List from, List to)
  8. isEqualAsMultiset(List left, List right)
  9. isEqualIgnoringOrder(List left, List right)