I am trying to store coordinates in a HashSet and checking whether a coordinate exist inside my set.
HashSet<int[]> hSet = new HashSet<>();
hSet.add(new int[] {1, 2});
hSet.add(new int[] {3, 4});
System.out.println(hSet.contains(new int[] {1, 2}));
>>> false
I am rather new to Java and from my understanding the output of the above is false is due to comparing the references of the int[] arrays rather than the logical comparison of their values. However using Arrays.equals() would not be making use of the hashing of the hashset as I would have to iterate over all its elements.
I have also read on other questions that it is not recommended to use arrays inside collections.
So if I wish to store coordinate pairs inside a HashSet, what data structure should I use so that I can search for an element using the hashcode?