Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element type of SortedSet allows for calculation of the successor of a given value

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?

like image 643
Dron4K Avatar asked Jun 24 '18 16:06

Dron4K


People also ask

What is a SortedSet?

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.

Does SortedSet allow null values?

Yes, you can.

Does class Hashset implements SortedSet?

Since SortedSet is an interface, it can be used only with a class which implements this interface.

Is SortedSet ordered?

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.


3 Answers

the element type allows for calculation of the successor of a given value

It all depends on the sorting method

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).

like image 56
xtratic - Reinstate Monica Avatar answered Oct 04 '22 06:10

xtratic - Reinstate Monica


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.

like image 31
Mureinik Avatar answered Oct 04 '22 04:10

Mureinik


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.

like image 26
xingbin Avatar answered Oct 04 '22 04:10

xingbin