• 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

HashMap question required

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen when you attempt to compile and run the following code snippet?

String str1, str2;
HashMap map = new HashMap();
str1 = (String) map.put("Color", "White");
str2 = (String) map.put("Color", "Blue");
System.out.println("Color : " + str1 + " " + "Color : " + str2);

Why:It will print - Color : null Color : White ??

Thanks!!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the API say for HashMap.put ?

The API for HashMap is here
[ October 09, 2004: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please hold back on this one folks!
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yes, you are right Barry...we need to keep this thing in mind.

The code in the example can be understood only if you have some practical knowledge, otherwise you need to understand it thoroughly.

String str1, str2;
HashMap map = new HashMap();
str1 = (String) map.put("Color", "White");
str2 = (String) map.put("Color", "Blue");
System.out.println("Color : " + str1 + " " + "Color : " + str2);

Why:It will print - Color : null Color : White ??


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Explanation of put method
=========================
public Object put(Object key,
Object value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


str1 = (String) map.put("Color", "White");
When the first time this method is run, it looks for a key whose toString value is "Color" and it does not find any such key so it returns null.

str2 = (String) map.put("Color", "Blue");
When the second time this method is run, it again looks for a key whose toString value is "Color" and now it finds a key already in the map with this name and it replaces the previous value associated with the key (which is "White") with "Blue". And it returns the previous value (which is "White") and it has been casted to String as shown in the code.

So this is how we get "null" and "white" while printing.

Bye
Kaps
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice answer Kaps.

This was the part I'd hoped that VINCE would find:

Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the HashMap previously associated null with the specified key.




But I had intended that VINCE got to find it out for himself.
[ October 10, 2004: Message edited by: Barry Gaunt ]
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry.

Even I hope he got to find it himself also, because it is very nice to understand the things on your own.

Vince, I didn't intend to you stop your self learning by answering to your question in this elaborate manner. But it was to help you and all others who are trying to learn this concept.

Best of Luck, Vincy

Kaps
 
reply
    Bookmark Topic Watch Topic
  • New Topic