• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections doubt(Niko's blog)

 
Eric Nadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
The following code gives the output 1 on compilation.



Why is the output 1?
Also when i tweaked the code a bit,



I got null 1 as output.I'am not able to understand what's happening!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from JavaDoc of java.util.Map#put

Returns:
the previous 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, if the implementation supports null values.)
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Javadocs, the put method returns

the previous 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.)


Source

Seetharaman bet me by few seconds.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked the documentation for TreeMap.put? See java.util.TreeMap#put(K, V). I think that explains it fairly well:

Returns:
the previous 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.)


Edit: too slow - sorry for the repetition!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow...hat-trick replies...
 
Eric Nadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Thanks.You guys are great! Perfectly understood
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a general rule, if you don't understand the behaviour of a class or method in the core libraries, the first port of call should be the Javadocs. If they don't explain it, or if you don't understand them, then that's the time to ask on a forum. Otherwise you risk being told to RTFM .
 
Eric Nadar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! Sorry!! Will read the Java docs first next time. Thanks!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic