Am I using HashSet.contains() wrong?
Well, you have an expectation which is wrong, but it's more about arrays than about HashSet. Arrays in Java don't implement equals and hashCode in the way it looks like you're expecting them to. They effectively exhibit reference equality behaviour. For example:
int[] array1 = { 1 };
int[] array2 = { 1 };
System.out.println(array1.equals(array1)); // true - same reference
System.out.println(array1.equals(array2)); // false - different reference
Rather than using an int[] for coordinates, I'd suggest creating a Coordinate class with x and y fields. If that Coordinate class then overrides equals and hashCode appropriately, then using HashSet will work. (Although I'd advise creating the object you're looking for outside the loop rather than in each iteration.)
java - Proper use of HashSet.contains()? - Stack Overflow
What is the complexity of contains method of HashSet?
java - How does contains-Method in HashSets work? - Stack Overflow
Understanding contains method of Java HashSet - Stack Overflow
Am I using HashSet.contains() wrong?
Well, you have an expectation which is wrong, but it's more about arrays than about HashSet. Arrays in Java don't implement equals and hashCode in the way it looks like you're expecting them to. They effectively exhibit reference equality behaviour. For example:
int[] array1 = { 1 };
int[] array2 = { 1 };
System.out.println(array1.equals(array1)); // true - same reference
System.out.println(array1.equals(array2)); // false - different reference
Rather than using an int[] for coordinates, I'd suggest creating a Coordinate class with x and y fields. If that Coordinate class then overrides equals and hashCode appropriately, then using HashSet will work. (Although I'd advise creating the object you're looking for outside the loop rather than in each iteration.)
HashSet contains() Method in Java
util. HashSet contains() method is used to check whether a specific element is present in the HashSet or not. So basically it is used to check if a Set contains any particular element.
As far as I know, it's o(1) but my professor told me it is o(n) He said that it makes a linear search in the set. I don't think it's correct. But I need some proof to show my professor to get my mark from the question. I made some searches and some guys on stackoverflow.com say that it's o(1). So in short how can I prove to my professor it o(1) to get a mark?
" All right guys, I objected and shared my search with him. He confirmed that it's o(1). there is a misunderstood. He taught it's an ArrayList because of my handwriting. So the problem is fixed. Thanks for your all effort."
If the hash code method is based on the name field, and you then change it after adding the object, then the second contains check will use the new hash value, and won't find the object you were looking for. This is because HashSets first search by hash code, so they won't bother calling equals if that search fails.
The only way this would work is if you hadn't overridden equals (and so the default reference equality was used) and you got lucky and the hash codes of the two objects were equal. But this is a really unlikely scenario, and you shouldn't rely on it.
In general, you should never update an object after you have added it to a HashSet if that change will also change its hashcode.
Since your new User has a different hashcode, the HashSet knows that it is not equal. HashSets store their items according to their hashcodes.
The HashSet will only call the equals method if it finds an item with the same hashcode, to make sure that the two items are actually equal (as opposed to a hash collision)