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.)

Answer from Jon Skeet on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ hashset-contains-method-in-java
Java HashSet contains() Method - GeeksforGeeks
July 11, 2025 - The contains() method of the HashSet class in Java is used to check if a specific element is present in the HashSet or not.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java โ€บ util โ€บ hashset_contains.htm
Java HashSet contains() Method
The method call returns 'true' if this set contains the specified element. ... The following example shows the usage of Java HashSet contains() method to check entries to be present in a HashSet.
Discussions

java - Proper use of HashSet.contains()? - Stack Overflow
I'm attempting to generate a maze using a version of Kruskal's algorithm. I need to check if some coordinates (in an int[] array, eg [1, 5]) are in an existing set. Here is a copy of the part of the More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is the complexity of contains method of HashSet?
It is O(1) in practice, but in the very very worst case (when you have objects with a broken hash function) it is O(n). It can be considered O(m) with m being the size of your object you're inserting: hash function calculation scales linearly with object size. But that's insignificant in practice, and not relevant when talking about Sets and Collections. More on reddit.com
๐ŸŒ r/java
33
3
December 9, 2021
java - How does contains-Method in HashSets work? - Stack Overflow
How does the contains-method find out if a certain object is/is not in a HashSet? I created a class with two instance variables. Then I put a few instances into a HashSet. I created the same instan... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Understanding contains method of Java HashSet - Stack Overflow
To quote Jon Skeet "Objects in ... been used in a hashset (or hashmap)." - stackoverflow.com/questions/4718009/โ€ฆ ... 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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Medium
rupen-anjaria.medium.com โ€บ what-is-hashset-in-c-646df468ced6
What is Hashset in C#?. In this article we will explore what isโ€ฆ | by Rupen Anjaria | Medium
August 18, 2024 - The UnionWith method modifies the current HashSet<T> to contain all elements that are present in either the current set or the specified collection.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ performance of contains() in a hashset vs arraylist
Performance of contains() in a HashSet vs ArrayList | Baeldung
April 4, 2025 - On average, the contains() of HashSet runs in O(1) time. Getting the objectโ€™s bucket location is a constant time operation.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c# โ€บ c-sharp-check-if-a-hashset-contains-the-specified-element
C# | Check if a HashSet contains the specified element - GeeksforGeeks
July 11, 2025 - // C# code to check if a HashSet // contains the specified element using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a HashSet of strings HashSet<string> mySet = new HashSet<string>(); // Inserting elements in HashSet mySet.Add("DS"); mySet.Add("C++"); mySet.Add("Java"); mySet.Add("JavaScript"); // Check if a HashSet contains // the specified element if (mySet.Contains("Java")) Console.WriteLine("Required Element is present"); else Console.WriteLine("Required Element is not present"); } }
Find elsewhere
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-hashsetcontains-function-in-java
What is the HashSet.contains() function in Java?
The contains() method of HashSet checks if the specified element is present in the HashSet or not.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ java โ€บ hashset-contains-method-with-example.aspx
Java HashSet contains() Method with Example
contains() method is used to check the existence of the given object (ob) in this HashSet.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ hashset-in-java
HashSet in Java - GeeksforGeeks
Creates a new HashSet containing the elements of the specified collection (removes duplicates automatically).
Published: September 1, 2026
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-hashset-contains
Java HashSet.contains() - Syntax & Examples
December 18, 2020 - HashSet.contains() Returns true if this set contains the specified element.
๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ what is the complexity of contains method of hashset?
r/java on Reddit: What is the complexity of contains method of HashSet?
December 9, 2021 -

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."

๐ŸŒ
Rust Programming Language
users.rust-lang.org โ€บ help
Using hashset.contains with tuple types without takeing ownership of the values - help - The Rust Programming Language Forum
October 2, 2021 - When you are storing a tuple of values, you can only call contains by passing a reference to a tuple that own the values. fn contains(s: &HashSet bool { s.contains(&v) } The issue is when you are doing a API and both the tuple and HashSet are some internal implementation you don't want to leak.
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ hashset in java
Java HashSet Guide: Manage Unique Elements with Ease
June 26, 2025 - You can check if an item exists in the HashSet using the contains() method. This method returns true if the item is there, and false if it is not.
๐ŸŒ
Tpoint Tech
tpointtech.com โ€บ java-hashset-contains-method
Java HashSet contains() Method - Tpoint Tech
March 17, 2025 - The contains() method of Java HashSet class is used to check if this HashSet contains the specified element or not.