Java Collection Compare compareCollections(Collection value1, Collection value2)

Here you can find the source of compareCollections(Collection value1, Collection value2)

Description

Compare two collections, handling null values as well.

License

Open Source License

Parameter

Parameter Description
value1 first value or null
value2 second value or null

Return

whether the values are identical or both null

Declaration

public static boolean compareCollections(Collection value1,
        Collection value2) 

Method Source Code

//package com.java2s;
/**/* w w w.j a  va  2 s . c  o m*/
 * Copyright (C) 2010 Orbeon, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */

import java.util.*;

public class Main {
    /**
     * Compare two collections, handling null values as well.
     *
     * @param value1    first value or null
     * @param value2    second value or null
     * @return          whether the values are identical or both null
     */
    public static boolean compareCollections(Collection value1,
            Collection value2) {
        // Add quick check on size, which AbstractList e.g. doesn't do
        return (value1 == null && value2 == null)
                || (value1 != null && value2 != null
                        && value1.size() == value2.size() && value1
                            .equals(value2));
    }
}

Related

  1. compare(final Collection o1, final Collection o2)
  2. compare(final Collection c0, final Collection c1, final Comparator c)
  3. compareAnyOrder(Collection c1, Collection c2)
  4. compareCollections(Collection a, Collection b)
  5. compareCollections(Collection cola, Collection colb)
  6. compareExactOrder(Collection c1, Collection c2)
  7. compareRef(final Collection left, final Collection right)
  8. compareSet(Collection s1, Collection s2)
  9. compareTo(Collection o1, Collection o2)