【问题标题】:Sortedset sublist printing排序集子列表打印
【发布时间】:2015-04-18 14:59:15
【问题描述】:

帮助我理解一件事。

我创建了一个子列表并打印出来。现在我不明白为什么要包含“apple”来打印,而“wind”却没有。如果我写“aa”,它会按我的意愿工作,但我想使用“apple”来创建子列表,其中不包括“apple”。这是真的吗?

SortedSet<String> set = new TreeSet<>();
set.add("apple");
set.add("key");
set.add("value");
set.add("roof");
set.add("size");
set.add("wind");

System.out.println(set);
System.out.println(set.subSet("apple","wind"));

输出:

[apple, key, roof, size, value, wind]
[apple, key, roof, size, value]

【问题讨论】:

  • 这就是那个方法的工作原理。See here
  • 哦,非常感谢。

标签: java sortedset


【解决方案1】:

看看documentation of subSet

参数:

  • fromElement - 返回集合的低端点(包括)
  • toElement - 返回集合的高端点(独占

所以subSet("apple","wind") apple 包含在结果中,但 wind 被排除在外。

如果您希望能够指定应包含或排除哪个端点而不是 SortedSet,您可以使用 NavigableSet 作为参考及其
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) 方法

NavigableSet<String> set = new TreeSet<>(Arrays.asList("apple", "key",
        "value", "roof", "size", "wind"));
System.out.println(set);
System.out.println(set.subSet("apple", false, "wind", false));

输出:

[apple, key, roof, size, value, wind]
[key, roof, size, value]

【讨论】:

    猜你喜欢
    • 2018-02-13
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    相关资源
    最近更新 更多