• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

HashMap, Overriding equals&HashCode

 
Ranch Hand
Posts: 62
Android PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello !

During my programming exercises for the Exam i constructed this code:


Now im wondering why line 17 returns false ?? Shouldnt it find the right key ? Or is my Equals/HashCode wrong ?

Thanks in advance !
 
Master Rancher
Posts: 5045
80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a bit confusing to see an empty, unused class called "Key", followed by another class called "Key_Override" which does not actually override the class Key. Or have any relationship with that class. At all. Or any other class. At all. Fortunately this stuff appears to be totally unrelated to anything. Perhaps next time, you could just leave this stuff out.

Torsten Oppermann wrote:


Um, OK. I don't know if it matters, but it's distracting me: why divide by 2 here? Aside from creating additional bug possibilities later in the code, I mean.

Torsten Oppermann wrote:


I would guess that the problem is with using == rather than .equals() in line 49 (in your original post). Objects (e.g. Strings) should usually be compared with .equals(), not ==. Unless you're sure you know what you're doing.
 
Torsten Oppermann
Ranch Hand
Posts: 62
Android PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Um, OK. I don't know if it matters, but it's distracting me: why divide by 2 here? Aside from creating additional bug possibilities later in the code, I mean.


i simply wanted to create an easy hashCode value which i can easily see

I would guess that the problem is with using == rather than .equals() in line 49 (in your original post). Objects (e.g. Strings) should usually be compared with .equals(), not ==. Unless you're sure you know what you're doing.


youre right and i fixed that, but it doesnt solve my understanding problem


 
Mike Simmons
Master Rancher
Posts: 5045
80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, when you "fixed that", did it also change the result from the .equals() method, or not?

Or, put another way: did it work?

Yes, I understand that you also want to understand how it works (or doesn't). But before anyone spends a lot of time on that question, it would be nice to know what the result was.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maybe I don't understand correctly your question , but how do you expect to return TRUE when you didn't put that key into the map?
You put it into hMap and you verify if it exist in map?





 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic