• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic

 
C Kushtawar
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 352
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Stephen Davies
Ranch Hand
Posts: 352
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruben, are we talking about the interface NavigableSet or its implementation as in TreeSet?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 352
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 352
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic