Java Array Equal equalIgnoreOrder(final Object[] array1, final Object[] array2)

Here you can find the source of equalIgnoreOrder(final Object[] array1, final Object[] array2)

Description

equal Ignore Order

License

Open Source License

Declaration

public static boolean equalIgnoreOrder(final Object[] array1, final Object[] array2) 

Method Source Code

//package com.java2s;
/*/*from  w w w  .  j  a  v  a 2s  . c  o  m*/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
Copyright (C) 2007 Marco Aurelio Graciotto Silva <magsilva@ironiacorp.com>
*/

import java.util.Arrays;

import java.util.Comparator;

public class Main {
    public static boolean equalIgnoreOrder(final Object[] array1, final Object[] array2) {
        final class HashComparator<T> implements Comparator<T> {
            /**
             * Compares its two arguments for ordering. Returns a negative integer, zero,
             * or a positive integer as the first argument is less than, equal to, or
             * greater than the second.
             *
             * We use this comparator because it does not depends upon any specific object
             * property. Any Java object has an hashCode().
             *
             * Note: this comparator imposes orderings that are inconsistent with equals.
             */
            public int compare(final T o1, final T o2) {
                if (o1 == null && o2 == null) {
                    return 0;
                }
                if (o1 == null && o2 != null) {
                    return -1;
                }
                if (o1 != null && o2 == null) {
                    return 1;
                }

                return (o1.hashCode() - o2.hashCode());
            }
        }

        if (array1 == null && array2 != null) {
            return false;
        }
        if (array1 != null && array2 == null) {
            return false;
        }
        if (array1 == null && array2 == null) {
            return true;
        }

        if (array1.length != array2.length) {
            return false;
        }

        final Object[] sortedArray1 = dup(array1);
        final Object[] sortedArray2 = dup(array2);
        Arrays.sort(sortedArray1, new HashComparator<Object>());
        Arrays.sort(sortedArray2, new HashComparator<Object>());

        return Arrays.equals(sortedArray1, sortedArray2);
    }

    /**
     * Create a copy of an array. It will not do a deep copy (the primitive values
     * and object references are copied to the targed array, but the objects refereed
     * by both arrays will be the same).
     *
     * @param array Array to be copied.
     *
     * @return Duplicated array.
     */
    public static Object[] dup(final Object[] array) {
        if (array == null) {
            throw new IllegalArgumentException(new NullPointerException());
        }

        final Object[] dupArray = new Object[array.length];
        System.arraycopy(array, 0, dupArray, 0, array.length);
        return dupArray;
    }
}

Related

  1. equal(byte[] a, byte[] b)
  2. equal(float[][] array1, float[][] array2)
  3. equal(int[] a, int[] b)
  4. equalByRows(boolean[][] a, boolean[][] b)
  5. equalFreqBinning(double[][] sampleData, int numPredVals, int numRespVals, int divInd)
  6. equalityCheck(byte[] a, byte[] b)
  7. equalMaps(Map expected, Map actual)
  8. equalOrDie(String testName, Object[] a, Object[] b)
  9. equals(byte[] a, byte[] b)