0

I am struggling on this one for quite some time now. I understand that if you are using hashset you must override hashcode and equals if you are dealing with objects different than String(Same strings have same hashcode I guess), and select which property to be taken in account, so the objects are unique by that property.

But whats is the deal with the hashmap? Do you need and when do you need to override hashcode() and equals() ? I am pretty sure you will tear me apart for that question, but if someone explains to me I will be more than happy. Tomorrow I have important test and this is something that's really bothering me!

I am asking particularly for hashmaps! For the hashset I think i understand the idea. I also read about hashcode() and hashfunction

Georgi Michev
  • 485
  • 2
  • 11
  • 26
  • 1
    How are the answers about HashSet not applicable for your question? – user202729 Feb 25 '18 at 13:30
  • (note that a question is duplicate of another if the answer solves that question, not if the two questions are identical) – user202729 Feb 25 '18 at 13:31
  • Alright so I must override 100% the hashcode and equals in a hashmap as well ? Not sure which property to use then – Georgi Michev Feb 25 '18 at 13:32
  • @GeorgiMichev See also: https://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java – lexicore Feb 25 '18 at 13:33
  • I still don't get it .. Lets say I use generate hashcode and equals from eclipse in a class when i have objects from that class in a hashmap. Which properties do I decide to mark in the options ? – Georgi Michev Feb 25 '18 at 13:44
  • A hash set is basically just a `HashMap` internally. It's the same principle. – Moira Feb 25 '18 at 14:11

1 Answers1

1

The answer is yes.

In Java you can add objects in collections. Let us say you wanted to find an object called A that you added to a list called L. Let us say this is an object that you defined with your own class and you override the method Object#equals(). When you are looping through list L you are testing if any of these objects are equal to object A. If the equals method returns true you have found your object.

When you add objects to any HashTable, HashMap or HashSet the hashcode method is used to generate a number. That number should as unique as possible. It is possible that objects of the same class have different values in their instance fields but their hashcode method produces the same value. If you have two objects X and Y of some class and they have the same hashcode and you put both of them in a HashMap Q they end up in the same bucket P. Let us say that P has two objects. Let say that you pass Q into a method with X and Y. The method wants to check if X exists in Q. Q will take X and get the hashcode. Q will use the hashcode to find a bucket. The bucket will be P. Bucket P has two objects. This is when the equals method is used to determine if the Bucket contains X by comparing each object in the bucket with X. If one of the object in the bucket matches X then X exists in Q.

shodz
  • 194
  • 2
  • 12