Java examples for Collection Framework:Array Equal
Array equals Ignore Order
/*//from www.j a va2 s . c o m * oxCore is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text. * * Copyright (c) 2014, Gluu */ //package com.java2s; import java.lang.reflect.Array; import java.util.Arrays; public class Main { public static void main(String[] argv) throws Exception { String[] values1 = new String[] { "1", "abc", "level", null, "java2s.com", "asdf 123" }; String[] values2 = new String[] { "1", "abc", "level", null, "java2s.com", "asdf 123" }; System.out.println(equalsIgnoreOrder(values1, values2)); } public static boolean equalsIgnoreOrder(String[] values1, String[] values2) { String[] valuesSorted1 = sortAndClone(values1); String[] valuesSorted2 = sortAndClone(values2); return Arrays.equals(valuesSorted1, valuesSorted2); } public static String[] sortAndClone(String[] array) { if (array == null) { return array; } String[] clonedArray = arrayClone(array); Arrays.sort(clonedArray); return clonedArray; } @SuppressWarnings("unchecked") public static <T> T[] arrayClone(T[] array) { if (array == null) { return array; } if (array.length == 0) { return (T[]) Array.newInstance(array.getClass() .getComponentType(), 0); } T[] clonedArray = (T[]) Array.newInstance(array[0].getClass(), array.length); System.arraycopy(array, 0, clonedArray, 0, array.length); return clonedArray; } }