GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from John Meyer's Mock 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 "Question from John Meyer Watch "Question from John Meyer New topic
Author

Question from John Meyer's Mock

Asif Iqbal
Greenhorn

Joined: May 02, 2008
Posts: 10
This code is from John Meyer's Mock and little bit edit by me,

import java.util.*;
class test{
public static void main( String args[] )
{
Map m = new HashMap();
String str = null;
m.put(new test() , "mill" );
m.put(new test() , "sill" );
m.put(new test() , "sill" );
m.put(null, "sill" );
String key = "key";
m.put(key, "value");
m.put(null, "sill" );
m.put(null, "sill" );
System.out.println(m.size());
System.out.println(m.get(null));
System.out.println(m.get(key));
Integer in = 5;
m.put(in,25);
m.put(12,26);
System.out.println(m.get(12));
System.out.println(m.get(in));
}
/*public boolean equals( Object o)
{
return true; // 1
}
public int hashCode()
{
return 0;
}*/
}

output is
5 (m.size())
sill (m.get(null))
value(m.get(key))
26 (m.get(12))
25 (m.get(in))

can anyone explain me , why is size 5? What is with key with int & Integer. If i comment both m.put(12,26) and m.put(in,25), size is still 5.
What are the behaviour of equals and hashCode methods. although both methods are commented, i can find my object through key?

thanks
Bhavik patel
Ranch Hand

Joined: Feb 20, 2007
Posts: 49
Hi Asif,

First of all, the code is printing size of Map before you add last two items in the map. That is why you are getting size 5.

Second, The equals and hashCode methods becomes critical when you use an object of class who or whose superclass implements these methods. As for example, in the same code you have posted, how do you retrieve first three values for which you have used boject of test class as a key?.


Bhavik Patel
Glassfish Consultancy service At http://www.jmatrix.in
Asif Iqbal
Greenhorn

Joined: May 02, 2008
Posts: 10
Hi Bhavik,

Thanks for your reply. String class overrides these methods, it means we can get Values associate with String key. but what happens if i add these lines

test t = new test();
m.put(t, "test reference");
System.out.println(m.get(t));#i

we are in test class and both equals and hashCode methods are still commented. I still get my value of key "t" at #i.
Bhavik patel
Ranch Hand

Joined: Feb 20, 2007
Posts: 49
hi,

Of course, you will get your value from the map because you are using the same reference to test object to find the value.

My point is if id do the following


and now I want to get the value back from this map. What should I use as a key ?
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Originally posted by Bhavik patel:
hi,

Of course, you will get your value from the map because you are using the same reference to test object to find the value.

My point is if id do the following


and now I want to get the value back from this map. What should I use as a key ?


U won't be able to get it by map.get();


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
 
GeeCON Prague 2014
 
subject: Question from John Meyer's Mock