Java Collection Equal isContentEqual(final Collection collectionA, final Collection collectionB)

Here you can find the source of isContentEqual(final Collection collectionA, final Collection collectionB)

Description

Method checks if the given collections contain the same content.

License

Apache License

Parameter

Parameter Description
collectionA the first collection
collectionB the other collection

Return

true if the content are equal, false otherwise.

Declaration

public static boolean isContentEqual(final Collection<?> collectionA, final Collection<?> collectionB) 

Method Source Code


//package com.java2s;
/*//from  w  ww .  j av  a  2  s.c  o m
 * TMQL4J - Javabased TMQL Engine
 * 
 * Copyright: Copyright 2010 Topic Maps Lab, University of Leipzig. http://www.topicmapslab.de/    
 * License:   Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * @author Sven Krosse
 * @email krosse@informatik.uni-leipzig.de
 *
 */

import java.util.Collection;

public class Main {
    /**
     * Method checks if the given collections contain the same content. Please
     * note that the method only check if each item of one collection is
     * contained at least one times in the other collection. Method does not
     * check if the number of containments of each element are equal. For
     * example this means that { A , B , B } and { A , A , B } are contained
     * equal content.
     * 
     * @param collectionA
     *            the first collection
     * @param collectionB
     *            the other collection
     * @return <code>true</code> if the content are equal, <code>false</code>
     *         otherwise.
     */
    public static boolean isContentEqual(final Collection<?> collectionA, final Collection<?> collectionB) {
        /*
         * check if at least one parameter are null
         */
        if (collectionA == null || collectionB == null) {
            throw new IllegalArgumentException("parameters can not be null.");
        }

        /*
         * check if both collection have the same size
         */
        if (collectionA.size() != collectionB.size()) {
            return false;
        }

        /*
         * check if each item of collection A contained in collection B
         */
        for (Object obj : collectionA) {
            if (!collectionB.contains(obj)) {
                return false;
            }
        }

        /*
         * check if each item of collection B contained in collection A
         */
        for (Object obj : collectionB) {
            if (!collectionA.contains(obj)) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. equalTestData(Collection receivedDataSet)
  2. getContainedEquals(Collection a, T b)
  3. indexOfEquals(Collection collection, T element)
  4. isCollectionEqual(Collection c1, Collection c2)
  5. isCollectionsEqual(Collection f1, Collection f2)
  6. isEqual(Collection left, Collection right)
  7. isEqual(Collection one, Collection two)
  8. isEqual(Collection coll, Comparator comp)
  9. isEqual(Collection a, Collection b)