This may seem like a weird question but I'm a little confused: In Java, the method NavigableSet.tailSet(Object) supposedly returns a SortedSet, and tailSet(Object, boolean) returns a NavigableSet. But it seems to me like they actually both return a TreeSet.
TreeSet<String> mts= new TreeSet<String>();
Object whatTypeAmI = mts.tailSet("bla");
I mean...
BUT:
Why is that?
EDIT:
I know that TreeSet implements NavigableSet which extends SortedSet which extends Set. I know it makes sense for a TreeSet to be recognized as a NavigableSet. But a NavigableSet shouldn't be recognized as a TreeSet.
The point of this question isn't so much practical use as it is theoretical. (I'm studying for a test. I need to be able to tell if code will compile or not). If tailSet returns a TreeSet, then how come this doesn't compile?
TreeSet<String> tree = mytreeset.tailSet("xx");
Why do I need to add a cast to TreeSet?
NavigableSet
and SortedSet
are both interfaces that are implemented by TreeSet
. Both methods could also return a ConcurrentSkipListSet
if they wanted to since it also implements both interfaces.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.