• 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

Question from John Meyer's Mock

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic