4

What does this idiom means? A java string concatenated with "\0" (e.g. "Japan\0")? As in

SortedSet<String> countryHeadSet 
    = countrySet.headSet("Japan\0");

What does the string "Japan\0" mean in the call? Would it make any difference if the programmer writes just

countryHeadSet 
    = countrySet.headSet("Japan");
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
leeyuiwah
  • 6,562
  • 8
  • 41
  • 71
  • Have you looked at this question ? http://stackoverflow.com/questions/9753576/whats-the-meaning-of-char-zero-0-in-java – Sandip Subedi Jan 29 '17 at 23:01
  • the `'\0'` char is `null` char – Mohsen_Fatemi Jan 29 '17 at 23:03
  • Actually, it's the character `NUL`, as a `char`, known as the "null character", but not `null`, which is a Java keyword and doesn't apply to primitive types. I bring it up because the words are so similar, but the meanings so different. https://en.wikipedia.org/wiki/Null_character https://unicode-table.com/en/ https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-EscapeSequence – Lew Bloch Jan 29 '17 at 23:13
  • Hi, Thanks for all the help. I have found the answer and provided it below. Thanks! – leeyuiwah Jan 29 '17 at 23:35

1 Answers1

4

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]
leeyuiwah
  • 6,562
  • 8
  • 41
  • 71