Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interview Question on java hashcode()

I recently attended an interview and was asked the following question.

There are two objects with same hashcode. I am inserting these two objects inside a hashmap.

hMap.put(a,a); hMap.put(b,b);

where a.hashCode()==b.hashCode()

Now tell me how many objects will be there inside the hashmap?

I answered there will be only one object,since the hashcodes are equal the two objects will be equal and hashmap will not allow duplicate keys. Please tell me whether my understanding is correct or not?

like image 799
Ammu Avatar asked Jul 14 '11 10:07

Ammu


People also ask

What is the purpose of the hashCode () method?

The purpose of the hashCode() method is to provide a numeric representation of an object's contents so as to provide an alternate mechanism to loosely identify it. By default the hashCode() returns an integer that represents the internal memory address of the object.

What happens if hashCode () method always return same value?

If multiple objects return the same value from hashCode(), it means that they would be stored in the same bucket. If many objects are stored in the same bucket it means that on average it requires more comparison operations to look up a given object.

What happen if 2 objects hashCode is same?

HashCode collisions Whenever two different objects have the same hash code, we call this a collision. A collision is nothing critical, it just means that there is more than one object in a single bucket, so a HashMap lookup has to look again to find the right object.

Can 2 strings have same hashCode?

Two same strings/value must have the same hashcode, but the converse is not true. There might be another string which can match the same hash-code, so we can't derive the key using hash-code. The reason for two different string to have the same hash-code is due to the collision.


4 Answers

There can be two different elements with the same hashcode. So your answer is incorrect. The only thing guaranteed is that if two elements have different hashcodes then they are different. When two elements have the same hashcode then Java uses the equals to further differentation.

So the answer is one or two objects.

like image 71
Petar Minchev Avatar answered Sep 18 '22 20:09

Petar Minchev


There will either be one or two objects in the hashmap.

If the two objects are not equal, ie !a.equals(b), both will be stored.

like image 23
Tom Jefferys Avatar answered Sep 17 '22 20:09

Tom Jefferys


No, the hashCode is an initial lookup for efficiency, but unless a.equals(b)==true there will be two entries

like image 24
Sean Patrick Floyd Avatar answered Sep 17 '22 20:09

Sean Patrick Floyd


There will be two objects in the hashmap, because they are not equals().

Here's the proof:

public static void main(String[] args) {
    Object a = new Object() {
        public int hashCode() {
            return 1;
        }
    };

    Object b = new Object() {
        public int hashCode() {
            return 1;
        }
    };

    Map<Object, Object> map = new HashMap<Object, Object>();
    map.put(a, a);
    map.put(b, b);
    System.out.println(map.size());
}

Output:

2

If I add an equals() method like this:

        public boolean equals(Object obj) {
            return obj.hashCode() == hashCode();
        }

Output:

1

According to the javadoc for Object.equals():

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

This does not however mean that two objects that aren't equals() can't share the same hashcode.

like image 27
Bohemian Avatar answered Sep 21 '22 20:09

Bohemian