This is how the Java standard library designers have designed this class. If the subset accepts an element that doesn't fall within that range, the subset will no more satisfy the condition of that subset. Each element of that subset must be within that range. You should go through the JavaDoc API description of this method.
If you like our products and services, please help us by posting your review here.
Is there a reason why the 2 argument subset returns a SortedSet, and the 4 argument subset returns a NavigableSet? (Same applies for both TreeMap.submap() methods, and also for the pair of tailSet/tailMap methods - when the boolean params are included, return is Navigable, otherwise - just Sorted.)
Good observation. The hierarchy of sets is like this: Set <- SortedSet <- NavigableSet <- TreeSet. The two parameter subset method is declared in SortedSet, so obviously, it can't return NavigableSet. The four parameter subset method is declared in NavigableSet and it returns the most specific Set type that it can, which is NavigableSet.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.