File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic" Watch "Generic" New topic
Author

Generic

C Kushtawar
Ranch Hand

Joined: Jan 20, 2009
Posts: 37
This is a question from Javabeats.



The correct answer is a,b.

Please expln how is b correct here?I think only a is correct.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
NavigableSet is an interface, so i would say a, is a questionable answer from one perspective. However, from the perspective of answer b, NavigableSet interface is mainly implemented by TreeSets (There is also NavigableMap for TreeMaps). Looking at NavigableSet its methods work on ranges of data such as lower() and higher() so it would make no sense to call these on null values, after all what would higher(null) actually produce?

This is just a guess

be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
As far as I know there is no rule that says that you can't have the null element in a set (So Answer B is wrong.) The only problem is that if the set is ordered using natural ordering, having the null value will throw a NullPointerException at runtime. But if you use a Comparator instance that provides for the null element, then you won't have any problems. Here is an example:


All code in my posts, unless a source is explicitly mentioned, is my own.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Ruben, are we talking about the interface NavigableSet or its implementation as in TreeSet?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi Stephen,

TreeSet implements NavigableSet, so if there were any restriction to add null elements to a NavigableSet, I think that would also be present in TreeSet. I couldn't find mention of any such restriction in the NavigableSet API documentation either. Have you read anywhere about this restriction?
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
No, nothing, I was taking a stab in the dark, based on the methods, but there's nothing to support my guess!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
The main thing is that all elements must be mutually comparable. If you use natural ordering, null will give you problems. But if you use an acceptable Comparator, then it won't be an issue.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Well I suppose if you try and add more than one null, your going to get issues, but that doesn't stop you from adding at least one! Maybe one of the Sheriff's may be able to shed more light on the issue?

 
 
subject: Generic