wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Using classes as Keys in Map  problem-2 Unexpected OUTPUT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Using classes as Keys in Map  problem-2 Unexpected OUTPUT" Watch "Using classes as Keys in Map  problem-2 Unexpected OUTPUT" New topic
Author

Using classes as Keys in Map problem-2 Unexpected OUTPUT

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



gives:

hashcode
this isTuesday which equals Monday with valuefalse
this isMonday which equals Monday with valuetrue
get value corr key t1 payBills


---------------------------------------------------------

gives:

hashcode
this isTuesday which equals Monday with valuefalse
get value corr key t1 payBills

why in't the output for m.get(t1) and m.get(t2) same



OCPJP 6.0 93%
OCPJWCD 5.0 98%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Could you please remove the unused lines from your code? We can't read them!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Abimaran
wrote:
Could you please remove the unused lines from your code? We can't read them!



Removed the unused lines
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Check you code, your t1 and t2 are same, the the value pair in the map will be overridden. Did you check your Map size?
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Abimaran wrote :
Check you code, your t1 and t2 are same, the the value pair in the map will be overridden. Did you check your Map size?


i know about that.
code-1


output of code-1:
====================================
hashcode
this isTuesday which equals Monday with valuefalse
this isMonday which equals Monday with valuetrue
get value corr key t1 payBills
=====================================

why the bold line not in the output of the code-2.

code-2



output of code-2
=====================================
hashcode
this isTuesday which equals Monday with valuefalse
get value corr key t1 payBills
=====================================

During get(),it goes to equals method for matching strings.


Then,why doesnot compiler displays
this isMonday which equals Monday with valuetrue
in the output of code-2
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I don't understand your question! There are two keys in your Map, so, they will be used in your equals method. Please ask the question specifically!
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

when m.get(t1) or m.get(t2)

Program Control to hashCode method to check the hashcode ,then goes to equals method to see if object passed to retrieve the value is same as used when value was put in map.


when we use t1 as key to get values associated with it

hashcode
this isTuesday which equals Monday with valuefalse
this isMonday which equals Monday with valuetrue
get value corr key t1 payBills

when we use t2 as key to get values associated with it

hashcode
this isTuesday which equals Monday with valuefalse
get value corr key t1 payBills

why compiler isn't displaying true when match is found in case of t2

where is the line "this isMonday which equals Monday with valuetrue" in second output


Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I got it, check your output carefully!
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i have checked it ....
not getting the reason for it

please Abimaran ,can you explain it

 
GeeCON Prague 2014
 
subject: Using classes as Keys in Map problem-2 Unexpected OUTPUT