8

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?

2
  • 5
    You can't answer the question without knowing whether a.equals(b) so this should be your response. Commented Jul 14, 2011 at 11:08
  • Depends on the element type and how hashCode is implemented. Imagine a map of strings. There are only 2^32 different hash codes, but a lot more possible string values. So it is impossible for every string value to have a unique hash code. Commented Jul 14, 2011 at 12:03

5 Answers 5

32

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Readers - check out the source code of HashMap.put(K, V) for evidence. +1 for the answer.
3

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.

Comments

2

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

Comments

1

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.

5 Comments

This is not correct. There can be two different objects with the same hashcode.
This is incorrect. The equals method is used to determine object identity in HashMaps.
@Bohemian - Again not perfectly correct. You don't know the outcome of equals. It may be overriden and then it will not compare references.
Guys, this is exactly what he said: "... because they are not equals()."
@AlexR - But he doesn't know that:) With the last edit it is OK and I will remove my downvote:)
0

There are either one or two key objects depending on a.equals(b), and either one or two value objects depending on a==b.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.