Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if element exists in HashSet by its hash

Can I check if an object is in a HashSet if I have only the object's hash code, but not the object itself?

like image 686
alkurmtl Avatar asked Feb 23 '15 17:02

alkurmtl


People also ask

How do I find a hash Set?

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.

Does HashSet use hashing?

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.

How do you compare two hash sets?

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.

How to check if an object exists in a HashSet?

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.

Which method is used to check whether a hashset contains an element?

HashSet.Contains (T) Method is used to check whether a HashSet object contains the specified element.

Why does the contains method of a HashSet return false?

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.

What is the difference between list and HashSet in Java?

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.


3 Answers

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.

like image 196
Eran Avatar answered Oct 13 '22 02:10

Eran


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;
    }
};
like image 30
Evgeniy Dorofeev Avatar answered Oct 13 '22 01:10

Evgeniy Dorofeev


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 :

  1. What house do I go to?
  2. Who do I find?

Here (1) will be answered by hashcode and (2) will be answered by equals method.

like image 1
Chetan Kinger Avatar answered Oct 13 '22 02:10

Chetan Kinger