• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on hasMap

 
Manoj Gundawar
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was about to upload my assignment. But I looked at one of the post here about the failure cause of locking issue. I guess my locking design is similer to the one posted there. So just wanted to make final checks before subimitting my assignment.
I am using HashMao to store the locked record no. Key is record no. and the value is cookie (owner of the record. Its a long no.)
Further in the Lock method I have following check:

Is it safe to check the new Integer(recNo) against the Integer rec present in the hashMap? Cause I am checking one object agianst other and not sure how hashMap's containsKey works. I checked the HashMap class's source code, but could not make out much from it.
Or do I need to traverse the entire hashMap, get the each Key, convert it to primitive int and compare with my primitive int recNo??
Someone told me that only Optimized JVM works correct for the above mentioed code as such JVMs will reuse the same object if the value is a constant value. But this may fail on non optimized jvms. I am not sure what it means.
Please comment.
Thanks,
Manoj
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manoj,
You should be fine with your current code.
From memory a HashMap works by getting the hashcode of the key, then traversing all the items with that hashcode to see if they are the object that you are looking for.
The two Integers you create for the same int value should have the same hashcode and should equate as true using the .equals() function. So for all practical purposes, the two keys will behave identically even though they are two distinct objects.
Regards, Andrew
 
Manoj Gundawar
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andrew.
Good that you are always around. Makes our life easier.
Manoj
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic