aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections doubt(Niko's blog) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections doubt(Niko Watch "Collections doubt(Niko New topic
Author

Collections doubt(Niko's blog)

Eric Nadar
Greenhorn

Joined: Oct 12, 2012
Posts: 12
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: May 25, 2009
Posts: 296

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.


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

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

Joined: Jan 28, 2008
Posts: 5575

wow...hat-trick replies...
Eric Nadar
Greenhorn

Joined: Oct 12, 2012
Posts: 12
Wow! Thanks.You guys are great! Perfectly understood
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

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

Joined: Oct 12, 2012
Posts: 12
Oh! Sorry!! Will read the Java docs first next time. Thanks!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Collections doubt(Niko's blog)