This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from John Meyer's Mock

 
Asif Iqbal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?.
 
Asif Iqbal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic