• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Generic

 
C Kushtawar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic