From SortedSet documentation:
several methods return subsets with restricted ranges. Such ranges are half-open, that is, they include their low endpoint but not their high endpoint (where applicable). If you need a closed range (which includes both endpoints), and the element type allows for calculation of the successor of a given value, merely request the subrange from lowEndpoint to successor(highEndpoint).
Can you explain what means
the element type allows for calculation of the successor of a given value
What types allow for calculation of the successors in Java?
A SortedSet is a Set that maintains its elements in ascending order, sorted according to the elements' natural ordering or according to a Comparator provided at SortedSet creation time.
Yes, you can.
Since SortedSet is an interface, it can be used only with a class which implements this interface.
Interface SortedSet<E> A Set that further provides a total ordering on its elements. The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time. The set's iterator will traverse the set in ascending element order.
the element type allows for calculation of the successor of a given value
It means that, for the sorting method on your elements, you can calculate what sorted value would come directly after your given value, with nothing possibly between them.
From the docs:
For example, suppose that s is a sorted set of strings. The following idiom obtains a view containing all of the strings in s from low to high, inclusive:
SortedSet<String> sub = s.subSet(low, high+"\0");
For Strings: (natural sort) high + "\0"
is the successor to high
For Integers: (natural sort) high + 1
is the successor to high
. But if your Integers were sorted from high to low, then the successor would be high - 1
.
For some values computing the successor is slightly more complicated...
For Doubles: (natural sort) Math.nextAfter(high, Double.POSITIVE_INFINITY)
is the successor to high
since nextAfter
gets the adjacent value after high
such that nothing could come between high
and nextAfter(high..)
. Note that you might run into trouble with max/min values or neg/pos infinity values for doubles, so you would probably want to check high
first
With real world floating point numbers this would not work (unless you set some limit to the precision).
This only works here because in computers floating point numbers always without exception have limited precision and thus you can calculate the next possible value in that precision (which is what nextAfter
does).
Allowing calculation of successors require your type to have discrete values (although that is not sufficient).
Integer
is a good example of this - the successor of 2
is 3
. The successor of 3
is 4
.
For example, the whole set contains 1, 3
, and you want to get Integer from between [1, 3], if you directly call
s.subSet(1, 3);
then 3 will not be in the subset.
In this situation, you can calculate the next element after 3 by 3 + 1 = 4
and call:
s.subSet(1, 4);
then 3 will be in the subset.
The calculation mechanism might differes from class to class. With Numberic
elements or String
, you can calculate the successor by +
directly. If you are manipulating on other type, you can custom your own calculation method, and it should be consistent with compare method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With