• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Sierra & Bates scjp 6 page 587

 
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to understand an example from k&b concerning TreeSets using only Java 5 code.
Cutting out the bits that are not relevant here, the code looks like this:



I don't understand line 18.
The docs say that TreeSet<Integer>.headSet() will return a SortedSet<Integer>
yet apparently it's acceptable to cast that returned object to a TreeSet
Just because an object implements the SortedSet<Integer> interface, how does that guarantee that its castable to a TreeSet?
Would it not be possible to construct somehow an object implementing SortedSet<Integer> that isn't so castable?
Or, does this line of code rely on some insider knowledge, beyond what's available in the docs, that TreeSet<Integer>.headSet() will actually return a TreeSet?

Thanks for any help,

Richard


 
Rancher
Posts: 4327
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like a bad practice to me. As far as I know there's no documented reason to expect headSet() to return a TreeSet here. In practice that is what TreeSet does, and it's possible to determine this either by experiment or by reading the source code. But it seems bad practice to depend on this. Better to simply declare the type as SortedSet everywhere - or, since JDK 1.6, NavigableSet, which is an improved version of SortedSet that TreeSet also implements.
 
Richard Hayward
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Seems like a bad practice to me. As far as I know there's no documented reason to expect headSet() to return a TreeSet here. In practice that is what TreeSet does, and it's possible to determine this either by experiment or by reading the source code. But it seems bad practice to depend on this. Better to simply declare the type as SortedSet everywhere - or, since JDK 1.6, NavigableSet, which is an improved version of SortedSet that TreeSet also implements.



Thanks for your comments Mike.
Seems I was justified in being uneasy.

Regards

Richard
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic