1、第一种方案
public static boolean compare(String[] array1, String[] array2) {
Set<String> aTarget = new HashSet<String>();
Set<String> bTarget = new HashSet<String>();
aTarget.addAll(Arrays.asList(array1));
bTarget.addAll(Arrays.asList(array2));
if (aTarget.size() != bTarget.size()) {
return false;
} else {
int temSize = aTarget.size();
aTarget.addAll(Arrays.asList(array2));
return temSize == aTarget.size();
}
}
2、如果快速比较两个string[] 无序相等 比如 new String[]{"aaa","bbb","ccc"} 和 new String[]{"bbb","ccc","aaa"}无序相等
@Test
public void should_return_false_when_not_all_item_equal() {
String[] array1 = new String[] {
"1", "2", "3"
};
String[] array2 = new String[] {
"2", "2", "2"
};
assertEquals(false, StringArrayCompare.compare(array1, array2));
}
@Test
public void should_return_true_when_all_item_equal() {
String[] array1 = new String[] {
"1", "2", "3"
};
String[] array2 = new String[] {
"3", "2", "1"
};
assertEquals(true, StringArrayCompare.compare(array1, array2));
}