Skip to main content
edited tags
Link
Youcef LAIDANI
  • 60.3k
  • 21
  • 111
  • 178
Source Link
Thomas
  • 1.9k
  • 1
  • 17
  • 34

java Set<int[]> vs Set<List<Integer>>

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