I have following code to use int[]
and List<Integer>
as hashset element, however, they have different result. Why List<Integer>
could be used to compare hash, but array not?
Set<List<Integer>> set2 = new HashSet();
set2.add(Arrays.asList(1, 2, 3, 4));
System.out.println(set2.contains(Arrays.asList(1, 2, 3, 4)));
int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {1, 2, 3, 4};
Set<int[]> set3 = new HashSet();
set3.add(arr1);
System.out.println(set3.contains(arr2));
The output is
true
false