0

I have created hash map and when I debug it I saw that I have duplicte keys. I didnt override the hashCode() & equals(Object obj) in the key - Object1 and i wonder how will it affect the performance of the map search?

private HashMap<Object1,Object2> map = new HashMap<Object1,Object2>();
4
  • If you have duplicate keys... remove one? Commented Dec 20, 2012 at 16:12
  • 6
    Hi, I wanted to build a road to Rome, but I've built one going somewhere else accidentally – will that take me longer or not? Commented Dec 20, 2012 at 16:13
  • i dont think you should have dupliate keys, if you allow for that you might get wrong entry from your map Commented Dec 20, 2012 at 16:16
  • 1
    You can't have duplicate keys. You may think you do, but you don't. Commented Dec 20, 2012 at 16:16

4 Answers 4

4

It is not possible to have duplicate keys in a Map, you have different keys that "appear" the same (Maybe based on their toString()? )because you have not overridden equals() and hashCode(), but in reality the keys are different.

This means that in order to get all values from your Map you need to keep every key you created and store it somewhere, which to me defeats the purpose of the Map.

Summary:

Override equals() and hashCode(), then put your key/value pairs into the Map.

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

3 Comments

I would say they don't "appear" the same, the OP just thinks they're duplicates.
@BrianRoach Well I can certainly see why they think they are the same, MyObject("A", "B") != MyObject("A", "B") if you are only using Object.equals(), but their toString() calls probably look identical. I added a line in response to your comment.
What I meant was that they only "appeared" to be the same to the OP; the HashMap certainly doesn't think they look alike at all ;) The underlying problem here is that the OP doesn't understand that his HashMap uses the same criteria for Object comparison as anything else which your answer covers.
0

It won't really give you bad performances (more the opposite) but it will prevent your objects from being seen as duplicate.

If you want each instance to be seen as a different key, don't override the equals and hashCode methods. But that means you'll need exactly the same instance to retrieve your value in the map.

If you want to retrieve the value with a different instance (for example with the same id), then you need to override the methods.

But the problem isn't really a performance one.

Comments

0

How can you have duplicate keys? You didn't override equals() neither hashcode() so you cannot say "duplicated objects", because that's exactly the purpose of theses methods. You probably debug and saw different values, but for the JVM they are not equals based on the default Object.equals() and Object.hashcode() (well actually the closest superclass)

Comments

0

You cannot have duplicate keys but you can have duplicate values. Might be you got confused with the key and value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.