Can I check if an object is in a HashSet
if I have only the object's hash code, but not the object itself?
HashSet contains() Method in Java 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. Parameters: The parameter element is of the type of HashSet.
HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage. A hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code.
The equals() method of java. util. HashSet class is used verify the equality of an Object with a HashSet and compare them. The list returns true only if both HashSet contains same elements, irrespective of order.
The contains method returns true if the HashSet contains the specified element, false otherwise. How to check if an object of a custom class exists in the HashSet? The HashSet contains method relies on the equals and hashCode methods to check if the set contains the specified element.
HashSet.Contains (T) Method is used to check whether a HashSet object contains the specified element.
For the HashSet of custom class objects, if the custom class does not override the equals and hashCode methods then the contains method fails to find the specified object. As we can see from the output, even if the account with id 101 exists in the HashSet object, the contains method returned false.
As far as performance is concerned, it is better in comparison to the list. HashSet .Contains (T) Method is used to check whether a HashSet object contains the specified element. Here, mySet is the name of the HashSet and item is the required element to locate in the HashSet object.
You can't, since the hashCode
is only used to locate the bin in which the object is stored within the HashSet
. There may be multiple elements in the same bin, and HashSet
uses equals
to determine if the searched element exists in the bin that the hashCode
was mapped to.
The bin may contain multiple elements having the same hashCode
, so knowing only the hashCode
is not enough.
You can make a special object, something like this
int hashCode = 1; // your
new Object() {
@Override
public boolean equals(Object obj) {
return true;
}
@Override
public int hashCode() {
return hashCode;
}
};
No you cant. Both equals and hashcode are required for finding an object in a hashed collection. The hashcode method tells which bucket to find the object in and the equals method finds the object in the bucket.
A real world example would be a house with many people in it. If you tell me to go find someone in some house, I am going to ask you two questions :
Here (1) will be answered by hashcode and (2) will be answered by equals method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With