I've found the answer. Thanks for all the ideas.
One of the use of the idiom of a string + "\0"
, especially when you see it
with a range-view operation of a SortedSet
, is to use it to find the
successor of the string.
Such a successor is needed because those range-view operation (subSet()
, headSet()
, and tailSet()
) provide half-open intervals, and you
may need the successor of a string to construct a closed interval. This is explained in the Java Doc of Sorted Set
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). 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 sub = s.subSet(low, high+"\0");
In other words, the difference between the two calls below:
countrySet.headSet("Japan\0");
countrySet.headSet("Japan")
is that the first one would get all the strings in countrySet
in the range from the beginning of the set, up to and including
the string "Japan"
(i.e. the range is a closed interval); while
the second would get all the strings in the range from the
beginning of the set, up to but not including
the string "Japan"
(i.e. the range is a half-open interval).
I am including a test program of my own to demonstrate the use of the
idiom:
import java.util.Arrays;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
public class StringPlusNull {
public static void main(String[] args) {
List<String> countryList
= Arrays.asList("U.S.", "U.K.", "China", "Japan", "Korea");
SortedSet<String> countrySet
= new TreeSet<>(countryList);
String toElement = "Japan";
SortedSet<String> countryHeadSet
= countrySet.headSet(toElement);
System.out.format("There are %d countries in the "
+ "(half-open) headset [, %s): %s%n"
, countryHeadSet.size(), toElement, countryHeadSet);
toElement = "Japan\0";
countryHeadSet
= countrySet.headSet(toElement);
System.out.format("There are %d countries in the "
+ "(half-open) headset [, %s): %s%n"
, countryHeadSet.size(), toElement, countryHeadSet);
}
}
The output would be:
There are 1 countries in the (half-open) headset [, Japan): [China]
There are 2 countries in the (half-open) headset [, Japan ): [China, Japan]