TreeSet of String 对我来说不合逻辑
TreeSet of String Illogical for me behaviour
我有这个代码:
public static void main(String[] args)
{
NavigableSet<String> myset = new TreeSet<String>();
myset.add("a");
myset.add("aa");
myset.add("b");
myset.add("bb");
myset.add("c");
myset.add("cc");
System.out.println(myset.ceiling("a"));
System.out.println(myset.floor("aaa"));
System.out.println(myset.higher("a"));
System.out.println(myset.lower("bb"));
}
我以为它会打印:a,b,null,c
。因为例如 higher("a")
应该给出 null
,因为 a 是这棵树的根,但它却给出 aa
。 higher
的定义是Returns the least element in this set strictly greater than the given element, or null if there is no such element.
所以严格来说大于a
就是大于a
,但是由于a
是三者的根,所以没有比它更大的了。 aa
按字母顺序排在 a
之后。
实际上它 returns 与我想要的正好相反。这是为什么。这对我来说似乎很不合逻辑。有什么想法吗?
Because for example higher("a") should give null, because a is the root of this tree, but instead it gives aa.
哪个元素是树的根并不重要,但如果树是平衡的,它应该是中间值,例如'"bb"' 应该是根。
"aa" 是 "a" 之后的第一个,因为字符串以相同的字符开头但更长,所以这是预期的结果。
since a is the root of the three , there is nothing greater than it.
"a"
是 最小的 所以没有比它小的了。
it returns the exact oposite of what I want.
在数字中,我们从 0、1、2、3 等从左到右递增。
在字母表中,我们说 a、b、c、d 从左到右递增。即 a < b && a < c
令人困惑的是比较是 ASCIIbetical 换句话说,字母的 ASCII 码顺序很重要。 (更具体地说,编码为 UTF-16 时使用的代码)这意味着 '0' < '9' 和 '9' < 'A' 和 'A' < 'Z' 但 'Z' < 'a' 和 'a' < 'z'
我有这个代码:
public static void main(String[] args)
{
NavigableSet<String> myset = new TreeSet<String>();
myset.add("a");
myset.add("aa");
myset.add("b");
myset.add("bb");
myset.add("c");
myset.add("cc");
System.out.println(myset.ceiling("a"));
System.out.println(myset.floor("aaa"));
System.out.println(myset.higher("a"));
System.out.println(myset.lower("bb"));
}
我以为它会打印:a,b,null,c
。因为例如 higher("a")
应该给出 null
,因为 a 是这棵树的根,但它却给出 aa
。 higher
的定义是Returns the least element in this set strictly greater than the given element, or null if there is no such element.
所以严格来说大于a
就是大于a
,但是由于a
是三者的根,所以没有比它更大的了。 aa
按字母顺序排在 a
之后。
实际上它 returns 与我想要的正好相反。这是为什么。这对我来说似乎很不合逻辑。有什么想法吗?
Because for example higher("a") should give null, because a is the root of this tree, but instead it gives aa.
哪个元素是树的根并不重要,但如果树是平衡的,它应该是中间值,例如'"bb"' 应该是根。
"aa" 是 "a" 之后的第一个,因为字符串以相同的字符开头但更长,所以这是预期的结果。
since a is the root of the three , there is nothing greater than it.
"a"
是 最小的 所以没有比它小的了。
it returns the exact oposite of what I want.
在数字中,我们从 0、1、2、3 等从左到右递增。
在字母表中,我们说 a、b、c、d 从左到右递增。即 a < b && a < c
令人困惑的是比较是 ASCIIbetical 换句话说,字母的 ASCII 码顺序很重要。 (更具体地说,编码为 UTF-16 时使用的代码)这意味着 '0' < '9' 和 '9' < 'A' 和 'A' < 'Z' 但 'Z' < 'a' 和 'a' < 'z'