• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap contains key not working  RSS feed

 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason the contains key will produce false.. Can someone helps troubleshoot this.
inside my hashmap I take a object for the key, and an integer for the value

Here is the code for the key class:




and here is where the code is calling that is in error:



accessorHashmapFill2 is from another class that is used to interact with a database. I had did this with another key the same way and it worked fine. I just added another variable in this new key class, and now it wont work. My equals method works fine. It's when it calls the hashcode from .containskey is when it displays false, when it should be showing true.

Edited for simplicity.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If two objects are equal, they must produce the same hash code. If they don't, then you can get weird results when you store them in hashes. Are you sure the hashCode() method is fulfilling that contract?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jef Cho wrote:Here is the code for the key class:

Jef,

Please re-read the UseCodeTags page thoroughly.
Your code contains lots of lines that are far too long, and it makes things very difficult to read;
I suggest you split them up if you want people to answer. Normally I'd do it myself, but you have loads.

Also: On line 39, why not just return test.hashCode()?

Winston
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . .
Jef,

Please re-read the UseCodeTags page thoroughly.
Your code contains lots of lines that are far too long, and it makes things very difficult to read . . .
You don’t only have to think about Length of lines here; if you go for a job and have to hand in a printout of some of your work, lines that long will ensure you don’t get the job!
 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that.
Fixed the tags.

And sorry I'm new. I just finished a class in Java last semester new. What do you mean length of lines. Is my code not efficiently written? Or was I just printing out too many lines?
Or is one single line containing too much code and should be continued on another line? I take constructive criticism well to help better myself. I'm currently doing an internship, so I want
to make sure I'm doing things as efficient as possible.

Thank you.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jef Cho wrote:Or is one single line containing too much code and should be continued on another line?

Bingo. My post was specifically about using code tags here at Javaranch; Campbell's was about "general readability".

Us old farts don't like ploughing through enormously long lines; it hurts our eyes and fries our brains.

Winston

Oh, and by the way: you haven't fixed the problem. Read the page again: 100 characters MAX.
 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Jef Cho wrote:Or is one single line containing too much code and should be continued on another line?

Bingo. My post was specifically about using code tags here at Javaranch; Campbell's was about "general readability".

Us old farts don't like ploughing through enormously long lines; it hurts our eyes and fries our brains.

Winston


Understandable. I didn't even realize I was doing that though. My monitor here at work is like 23 inches or something so the screen is huge. Thanks
for the reminder though. And as always, for your guys time to troubleshoot. I did finally figure out my issue, in the if statement I was calling hm and
not hm2.. Another very rookie mistake

Thanks again everyone for the help and constructive criticism. At least I can take away a good lesson from this post.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Us old farts might have bigger screens than some other users. If somebody has to use a 1024×768 screen, rather than my tiny 11″ screen with 1368px from left to right, they will have to scroll left and right far more than I have to. I like to keep lines less than half the width of my screen.
Yes, if you have a long line, you can split it into several lines. I disagree about { at the end of the line, but have a look at these conventions.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jef Cho wrote:I did finally figure out my issue, in the if statement I was calling hm and not hm2.. Another very rookie mistake.


Actually... you didn't notice that tiny misprint, and that's something which isn't restricted to rookies. The mistake here is naming your variables "hm" and "hm2", which are meaningless. If you had named them something more meaningful, it would quite likely have made it harder to overlook that typo.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!