Help coderanch get a
new server
by contributing to the fundraiser
  • 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

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When i running this, getting
out put as - null : BBB

Could any one please explain what is happening in this code.

Thanks,
Anand
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that the output also includes BBB
 
anand-k jha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh....
Please replace C with A in this line as

Now the output coming as mentioned above

Thanks,
Anand
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output of your code should be, null : null

Here is the code for the output you specified.



See the docs.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now the output coming as mentioned above


Now that your code is fixed, can you tell us what you don't understand about Map's put method ?
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeel Ansari you are wrong the output is "null : BBB"

explanation: the put() method of the map returns the value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)

so in the first case when you do: map.put("A", "BBB"); under the key "A" there is still nothing so a null value is obtained. however in the second call of map.put("A", "CCC"); the return value is the value under the key "A" before replacing it with the "CCC" which happens to be "BBB".

think about it that the put() method will return the replaced value if there is an existing one, or null if no value is associated with that key.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
omar, I think you misunderstood Adeel He says that the output for the original code is "null, null", but that the expected output for the code he has posted will be "null, BBB".
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aaaaa well then hell yes he is completely correct ;)
 
anand-k jha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Omar,

But I am still confused ...

What we suppose to get when a map parsed to string....
Why in the 1st case map.put("A", "BBB");, nothing inserted & why "BBB" not replace with "CCC" in 2nd case.

Thanks,
Anand
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow..
I didn't know about it. Thanks to JavaRanch, was really an interesting discovery.

Ben
 
anand-k jha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Ben,

Also surprised when i was see output 1st time.

- Anand
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why in the 1st case map.put("A", "BBB");, nothing inserted


Why do you think that nothing is inserted ? Read carefully what the API says about the returned value.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Anand. Try this,
 
anand-k jha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still lots of confusions .......

Could any body clear it out.

- Anand
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anand-k jha wrote:Still lots of confusions .......

Could any body clear it out.

- Anand



put() method returns previous value associated by the key you are putting in the HashMap.

so when you call map.put("A", "BBB"); then put() first goes to find any previous value associated with the key "A", it finds "A" key is used first time, so it finds no value exists for "A" key, so it returns no value means null.
so s1=null;
And assigns "BBB" as value of key "A".
so map=[A=BBB]


Again put() goes to find any value associated with "A" key, this time it sees that "A" is already associated with "BBB" value, so it returns "BBB", so s2="BBB".
And now updates the value of key "A" in map.
so map=[A=DDD]
 
anand-k jha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you Punit & all,

Everything is clear now...
 
We begin by testing your absorbancy by exposing you to this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic