• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why am i getting a null value on fetching from map

 
utsav gupta
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written the following code which puts in three key value pairs in a map and then fetches one of them based on the key.



I am getting a null value while doing the get operation why??
I know that I have overridden the equals and hashcode functions of only the key class and not the value class. But it should still work because on doing the get operation only key class is called is that no correct?



[HENRY: Added code tags]
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Its the hashCode() method -- not the hashcode() method -- that you need to override.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you use the @Override annotation. That would have informed you about the typo in hashCode.
 
utsav gupta
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks Henry and Rob so much.
I would have never found that out. Can you please tell something more about @Override . I have never come across this before.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
utsav gupta wrote:Can you please tell something more about @Override . I have never come across this before.

From the doc,
Indicates that a method declaration is intended to override a method declaration in a supertype. If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:

* The method does override or implement a method declared in a supertype.
* The method has a signature that is override-equivalent to that of any public method declared in Object.


Thus apart from the advantage that Rob already mentioned, the other benefit you get while using this annotation is that if the parent class modified, the compiler will make sure that the child classes have been updated accordingly.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic