Why add the null character "\ 0" when looking for a headSet in a SortedSet?

What does this idiom mean? The java string associated with "\ 0" (for example, "Japan \ 0")? As in

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

What does the string "Japan\0"in the call mean ? Will it matter if the programmer just writes

countryHeadSet 
    = countrySet.headSet("Japan");
+4
source share
1 answer

I have found the answer. Thanks for all the ideas.

One of the uses of the + line idiom "\0", especially when you see it with an a SortedSetwith a rangefinder operation , is to use it to find the successor of the line.

, (subSet() , headSet() tailSet()) , . Java Doc of Sorted Set

. , , ( ). ( ), , lowEndpoint (highEndpoint). , , s . , s , :

SortedSet sub = s.subSet(, + "\ 0" );

, :

countrySet.headSet("Japan\0");
countrySet.headSet("Japan")

, countrySet , "Japan" (.. ); , , "Japan" (.. ).

, :

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);
    }

}

:

There are 1 countries in the (half-open) headset [, Japan): [China]
There are 2 countries in the (half-open) headset [, Japan ): [China, Japan]
+3

Source: https://habr.com/ru/post/1668208/


All Articles