1

I made a post a few days ago about using a HashMap in a simple banking program, but I'm having issues with using Objects as keys.

    HashMap <Account,Client> HM = new HashMap<Account, Client>();
    HM.put(new Account(2193,"Uri"), new Client(2193,0,"Uri"));
    HM.get(2193,"Uri");

Account and Client are classes in other parts of the source. My issue is that the HM.get isn't working as intended, and is giving me an error. Is there another way I'm to 'get' the value? Not sure how to use the key. Do note, the setup of the HashMap is without error.

Furthermore, is there a better way to go about this?

Uri D. Charles
  • 57
  • 1
  • 1
  • 9

2 Answers2

4

This will give you better idea. that why you need to override hashcode and equals method.

Why do I need to override the equals and hashCode methods in Java?

After overriding hashcode and equals method.

you need to use your object while getting data from hashMap.

HM.get(new Account(2193,"Uri"));
Community
  • 1
  • 1
Pratik
  • 944
  • 4
  • 20
  • If you think that a different SO question answer the question at hand you should mark it as duplicate, not just create an answer pointing to it. Furthermore even if the overrides those methods his code will not compile. On the other hand even without overriding them if used correctly `get` will work in this case. – Mateusz Dymczyk Apr 03 '15 at 04:53
  • @MateuszDymczyk Thanks for your advice. but i thot the guy is not aware about the why we need to override hashcode and equals mehtod. so thats why i point out the best example so he/she can learn the basic fundamentals. And question is different so that's why i have'nt marked it as a duplicate and try to give the way so he/she can be able to look further and the way you explain is really nice. Thanks for the points that you have mentioned. – Pratik Apr 03 '15 at 05:07
  • in that case posting this as a comment would be the way to go or answering his question and mentioning this in the answer as a side note – Mateusz Dymczyk Apr 03 '15 at 05:09
  • Will definetly take care this point from next onwards and will edit my answer and explain in detail. Thanks a lot again. – Pratik Apr 03 '15 at 05:10
1

First of all this code does not compile as you are passing 2 arguments to get() which expects only 1 argument.

That argument is supposed to be the key you use in the map and has to be of the same type you declared while declaring your map, in your case HashMap <Account,Client> HM means that HM (which btw should be lowercase by convention) holds as keys objects of type Account and objects of type Client as values.

It would still compile if you did:

get(2193)

Since get() takes an Object but it would simply return a null.

You need to do get(new Account(2193,"Uri")).

Next you do not need to override equals and hashCode in those classes but it is highly recommended (others already pointed to links saying why). Also as per the doc you should make the keys immutable so they do not change, otherwise you might get strange behavior.

Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map.

For more detailed description of the Map interface follow Oracle's tutorial

Mateusz Dymczyk
  • 14,969
  • 10
  • 59
  • 94