• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

collections topic(map)

 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the example of kathy siera book (generics and collection)-page-584



OUTPUT:
d key
null
d key
d key

why the last output i.e. //4 comes "d key" instead of "null" ???
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you put some things into a Hashmap, it invokes the hashCode() method of that key object, according that value, it makes hash buckets, in you case, it create a bucket correspond to the name length of your d object(6), there are possible to have many object in the same hash bucket. So then it invokes equals() method of the objects to determine whether those object are same or not. If same, it won't allow to put it in the Hashmap.

In your case, the value pair object is put in a hash bucket(with related to 6). When it search a value pair with the key object, first it check the key's hashCode() method, and determine, in which bucket it should check the corresponding object pair. So if you change the hash value of your key other than 6, it simply thinks that, there is no hash bucket related to this(changed key's hash value) hash value. So it indicates null. In your 25th line, you've changed the d's name, but you get the value pair with that(the one you changed), so it finds the corresponding hash bucket, it's there, and it invokes the equals() method. It returns the corresponding value pair.
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The HashMap.get() method first checks whether the key you are looking for is == before calling the equals() method. Since the d1 object you are looking for is in the HashMap then the == finds the object so there is no need to call equals(). You can verify this by adding System.out.println() in your equals() and hashCode() methods.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:The HashMap.get() method first checks whether the key you are looking for is == before calling the equals() method. Since the d1 object you are looking for is in the HashMap then the == finds the object so there is no need to call equals(). You can verify this by adding System.out.println() in your equals() and hashCode() methods.




yes TOM you are right ,equals() method doesn't invoked in line 25 ,only hashcode() method checks and verify about the object.

but as it is providing a wrong output i.e. "arthur" which is not in the hashmap, it stills retrieving a "d key".

so tell me how can i modify this program(any modification in hashcode() or equals() method) so that it provides us a valid and desirable answer??
thanks.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the below code first :


The outputs :

Then check this also...


The Output :



What do you say? (please read my first post in this thread.) [code mayn't be in textual order, forgive for it please!)

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:The HashMap.get() method first checks whether the key you are looking for is == before calling the equals() method. Since the d1 object you are looking for is in the HashMap then the == finds the object so there is no need to call equals(). You can verify this by adding System.out.println() in your equals() and hashCode() methods.



Could you please tell me the answer for my above post codes snips? Still I'm searching the object pair with the d1 key pair. According to your explanation, it shouldn't invoke the equals() method. But it does. Could you please explain the scenario?
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Could you please tell me the answer for my above post codes snips?

I didn't see a question.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:

Could you please tell me the answer for my above post codes snips?

I didn't see a question.







why is it like that?

tell me the logic behind the "when we are passing a reference i.e. key d1 in get() method,then only hashcode() method invokes, but when you are passing the object values(e.g. new d("clover")) in get() method then both equal and hashcode() methods invokes and verifies before providing output."

we know that if equals invokes it automatically invokes hashcode() method.

please tell us the logic why both method invokes in case of (new d("clover")) in get() but only hashcode in case of d1 passing in get() method?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

While this topic is interesting, keep in mind that you are violating a requirement of the hashmap -- and that is, the object used as a key isn't allowed to be changed after it has been placed into the map.

If you change the value of the key, such that the hashcode and/or the equals changes, then you corrupted the map -- key/value pairs may now be in the wrong bucket, and the no-duplicates requirement may also be violated. In effect, you broke the hashmap, and trying to figure out why something that is broken is behaving a particular way is not very useful.


But to answer your question, it looks like... in the first case, you broke the map to the point that the element is in the wrong location, and hence, can't be found. And in the second case, you got lucky and didn't break the map (even though you violated the requirement). And the reason you didn't break the map, was because the change didn't change the hashcode, because the string size didn't change.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic