为什么在 SortedSet 中查找 headSet 时附加空字符“\0”?
Why append the null character "\0" when looking for a headSet in a SortedSet?
这个成语是什么意思?与“\0”连接的 java 字符串(例如 "Japan[=17=]")?如
SortedSet<String> countryHeadSet
= countrySet.headSet("Japan[=10=]");
字符串"Japan[=12=]"
在调用中是什么意思?如果程序员只写
会有什么不同吗
countryHeadSet
= countrySet.headSet("Japan");
我找到了答案。感谢所有的想法。
一个字符串的成语用法之一"[=13=]"
,尤其是当你看到它的时候
用一个range-view操作一个SortedSet
,就是用它来找到
字符串的后继。
需要这样的继任者,因为那些 range-view 操作(subSet()
, headSet()
, 和 tailSet()
) 提供 half-open 间隔, 你
可能需要字符串的后继来构造一个 closed 区间。 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+"[=21=]");
也就是说下面两个调用的区别:
countrySet.headSet("Japan[=10=]");
countrySet.headSet("Japan")
是第一个会得到countrySet
中的所有字符串
在从集合开头到 包括 的范围内
字符串 "Japan"
(即范围是 封闭 区间);尽管
第二个将获取范围内的所有字符串
集合的开头,最多 但不包括
字符串 "Japan"
(即范围是 half-open 区间)。
我包括一个我自己的测试程序来演示
成语:
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[=11=]";
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]
这个成语是什么意思?与“\0”连接的 java 字符串(例如 "Japan[=17=]")?如
SortedSet<String> countryHeadSet
= countrySet.headSet("Japan[=10=]");
字符串"Japan[=12=]"
在调用中是什么意思?如果程序员只写
countryHeadSet
= countrySet.headSet("Japan");
我找到了答案。感谢所有的想法。
一个字符串的成语用法之一"[=13=]"
,尤其是当你看到它的时候
用一个range-view操作一个SortedSet
,就是用它来找到
字符串的后继。
需要这样的继任者,因为那些 range-view 操作(subSet()
, headSet()
, 和 tailSet()
) 提供 half-open 间隔, 你
可能需要字符串的后继来构造一个 closed 区间。 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+"[=21=]");
也就是说下面两个调用的区别:
countrySet.headSet("Japan[=10=]");
countrySet.headSet("Japan")
是第一个会得到countrySet
中的所有字符串
在从集合开头到 包括 的范围内
字符串 "Japan"
(即范围是 封闭 区间);尽管
第二个将获取范围内的所有字符串
集合的开头,最多 但不包括
字符串 "Japan"
(即范围是 half-open 区间)。
我包括一个我自己的测试程序来演示 成语:
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[=11=]";
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]