Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For good programming, In the Hasp Map programming, what value given to Key. As I know I can put any

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For good programming, In the Hasp Map programming, what value given to Key. As I know I can put any string
 
Sandeep Kumar Jakkaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please put your car key in it .... so that you can find it easily .....
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jacob deiter wrote:In the Hasp Map programming, what value given to Key. As I know I can put any string


Not only Strings, Any java object is acceptable. And null also possible. Check the API
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But be careful that you don't change the results of hashCode by changing some field, or your key cannot be found again with get().

Once you add an object to a hash map its hashCode should never change. If it needs to, remove the object from the map first, then change the object, then add it again.

For example:

The fix is really easy:

This is actually why String and the primitive wrapper objects are such good candidates for maps - they are immutable (i.e. they never change).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic