Java : TreeSet 示例

Java 中的 TreeSet (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 TreeSet<E> 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

基于 TreeMap 的 NavigableSet 实现。元素使用其自然顺序进行排序,或者根据使用哪种构造函数,使用在集合创建时提供的 Comparator 进行排序。 (机器翻译)

Class diagram

final var set = new TreeSet<String>();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true

set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3

System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("x")); // false
final var treeSet = new TreeSet<String>();
treeSet.add("ccc");
treeSet.add("ddd");
treeSet.add("aaa");
treeSet.add("bbb");

System.out.println(treeSet); // [aaa, bbb, ccc, ddd]

final var hashSet = new HashSet<String>();
hashSet.add("ccc");
hashSet.add("ddd");
hashSet.add("aaa");
hashSet.add("bbb");

System.out.println(hashSet); // [aaa, ccc, bbb, ddd]

Constructors

TreeSet ()

构造一个新的空树集,并按照其元素的自然顺序进行排序。 (机器翻译)

final var set = new TreeSet<String>();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true

set.add("b");
set.add("a");
set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
final var set = new TreeSet<Integer>();

set.add(100);
set.add(999);
set.add(-200);

System.out.println(set); // [-200, 100, 999]

TreeSet (Collection<? extends E> c)

构造一个包含指定集合中的元素的新树集,并按照其元素的自然顺序进行排序。 (机器翻译)

final var c = List.of("b", "a", "c");

final var set = new TreeSet<>(c);
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3

TreeSet (Comparator<? super E> comparator)

构造一个新的空树集,并根据指定的比较器进行排序。 (机器翻译)

final var src = List.of("bbb", "aaa", "ccc");

final var set1 = new TreeSet<String>();
set1.addAll(src);
System.out.println(set1); // [aaa, bbb, ccc]

final var set2 = new TreeSet<String>(Comparator.reverseOrder());
set2.addAll(src);
System.out.println(set2); // [ccc, bbb, aaa]

TreeSet (SortedSet<E> s)

构造一个包含相同元素并使用与指定排序集相同顺序的新树集。 (机器翻译)

final var s = new TreeSet<String>(Comparator.reverseOrder());
s.add("a");
s.add("b");
s.add("c");

System.out.println(s); // [c, b, a]

final var set = new TreeSet<>(s);
System.out.println(set); // [c, b, a]
System.out.println(set.size()); // 3

Methods

boolean add (E e)

如果指定元素尚不存在,则将指定元素添加到此集合中。 (机器翻译)

final var set = new TreeSet<String>();
System.out.println(set); // []

System.out.println(set.add("a")); // true
System.out.println(set); // [a]

System.out.println(set.add("b")); // true
System.out.println(set); // [a, b]

System.out.println(set.add("c")); // true
System.out.println(set); // [a, b, c]

System.out.println(set.add("a")); // false
System.out.println(set); // [a, b, c]

boolean addAll (Collection<? extends E> c)

将指定集合中的所有元素添加到此集合。 (机器翻译)

final var src = Set.of("a", "b", "c");

{
    final var set = new TreeSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.addAll(Set.of("d", "e", "f"))); // true
    System.out.println(set);  // [a, b, c, d, e, f]
}
{
    final var set = new TreeSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.addAll(Set.of("a", "b", "c"))); // false
    System.out.println(set); // [a, b, c]
}
{
    final var set = new TreeSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.addAll(Set.of("a", "c", "e", "f"))); // true
    System.out.println(set); // [a, b, c, e, f]
}

void addFirst (E e)

抛出 UnsupportedOperationException。 (机器翻译)

final var set = new TreeSet<>();
System.out.println(set); // []

try {
    set.addFirst("aaa");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

void addLast (E e)

抛出 UnsupportedOperationException。 (机器翻译)

final var set = new TreeSet<>();
System.out.println(set); // []

try {
    set.addLast("aaa");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

E ceiling (E e)

返回此集合中大于或等于给定元素的最小元素,如果不存在这样的元素,则返回 null。 (机器翻译)

final var set = new TreeSet<String>();
set.add("b");
set.add("d");
set.add("f");

System.out.println(set); // [b, d, f]

System.out.println(set.ceiling("a")); // b
System.out.println(set.ceiling("b")); // b
System.out.println(set.ceiling("c")); // d
System.out.println(set.ceiling("d")); // d
System.out.println(set.ceiling("e")); // f
System.out.println(set.ceiling("f")); // f
System.out.println(set.ceiling("g")); // null

void clear ()

从该集合中删除所有元素。 (机器翻译)

final var set = new TreeSet<String>();
System.out.println(set); // []

set.add("a");
System.out.println(set); // [a]

set.add("b");
System.out.println(set); // [a, b]

set.clear();
System.out.println(set); // []

Object clone ()

返回此 TreeSet 实例的浅表副本。 (机器翻译)

final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

final var cloned = set.clone();
System.out.println(cloned); // [a, b, c]
System.out.println(cloned.getClass()); // class java.util.TreeSet

Comparator<? super E> comparator ()

返回用于对此集合中的元素进行排序的比较器,如果此集合使用其元素的自然排序,则返回 null。 (机器翻译)

final var set = new TreeSet<String>();
System.out.println(set); // []

final var comparator = set.comparator();
System.out.println(comparator); // null
final var set = new TreeSet<String>(Comparator.reverseOrder());
System.out.println(set); // []

final var comparator = set.comparator();
System.out.println(Objects.equals(comparator, Comparator.reverseOrder())); // true
final var set = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
System.out.println(set); // []

final var comparator = set.comparator();
System.out.println(Objects.equals(comparator, String.CASE_INSENSITIVE_ORDER)); // true

boolean contains (Object o)

如果此集合包含指定元素,则返回 true。 (机器翻译)

final var set = new TreeSet<String>();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]

System.out.println(set.contains("")); // false
System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("c")); // true

System.out.println(set.contains("X")); // false
System.out.println(set.contains("abc")); // false

Iterator<E> descendingIterator ()

按降序返回此集合中元素的迭代器。 (机器翻译)

final var set = new TreeSet<String>();
set.add("aaa");
set.add("bbb");
set.add("ccc");

System.out.println(set); // [aaa, bbb, ccc]

final var iterator = set.descendingIterator();

System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(s -> {
    System.out.println(s);
});

// Result
// ↓
//-- forEachRemaining --
//ccc
//bbb
//aaa

NavigableSet<E> descendingSet ()

返回此集合中包含的元素的反向顺序视图。 (机器翻译)

final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

final var descSet = set.descendingSet();
System.out.println(descSet); // [c, b, a]

set.add("d");

System.out.println(set); // [a, b, c, d]
System.out.println(descSet); // [d, c, b, a]

descSet.remove("b");

System.out.println(set); // [a, c, d]
System.out.println(descSet); // [d, c, a]

E first ()

返回此集合中当前第一个(最低)元素。 (机器翻译)

final var set = new TreeSet<String>();
System.out.println(set); // []

set.add("b");

System.out.println(set); // [b]
System.out.println(set.first()); // b

set.add("c");

System.out.println(set); // [b, c]
System.out.println(set.first()); // b

set.add("a");

System.out.println(set); // [a, b, c]
System.out.println(set.first()); // a
final var set = new TreeSet<String>();
System.out.println(set); // []

try {
    set.first();
} catch (NoSuchElementException e) {
    System.out.println(e);
}

// Result
// ↓
//java.util.NoSuchElementException

E floor (E e)

返回此集合中小于或等于给定元素的最大元素,如果不存在这样的元素,则返回 null。 (机器翻译)

final var set = new TreeSet<String>();
set.add("b");
set.add("d");
set.add("f");

System.out.println(set); // [b, d, f]

System.out.println(set.floor("a")); // null
System.out.println(set.floor("b")); // b
System.out.println(set.floor("c")); // b
System.out.println(set.floor("d")); // d
System.out.println(set.floor("e")); // d
System.out.println(set.floor("f")); // f
System.out.println(set.floor("g")); // f

SortedSet<E> headSet (E toElement)

返回此集合中元素严格小于 toElement 的部分视图。 (机器翻译)

final var set = new TreeSet<String>();
set.add("x");
set.add("y");
set.add("z");

System.out.println(set); // [x, y, z]

final var headSet = set.headSet("z");
System.out.println(headSet); // [x, y]

set.add("a");

System.out.println(set); // [a, x, y, z]
System.out.println(headSet); // [a, x, y]

headSet.remove("x");

System.out.println(set); // [a, y, z]
System.out.println(headSet); // [a, y]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.headSet("a")); // []
System.out.println(set.headSet("b")); // [a]
System.out.println(set.headSet("c")); // [a, b]
System.out.println(set.headSet("d")); // [a, b, c]

NavigableSet<E> headSet (E toElement, boolean inclusive)

返回此集合中元素小于(或等于,如果包含为真)toElement 的部分视图。 (机器翻译)

final var set = new TreeSet<String>();
set.add("x");
set.add("y");
set.add("z");

System.out.println(set); // [x, y, z]

final var headSet = set.headSet("y", true);
System.out.println(headSet); // [x, y]

set.add("a");

System.out.println(set); // [a, x, y, z]
System.out.println(headSet); // [a, x, y]

headSet.remove("x");

System.out.println(set); // [a, y, z]
System.out.println(headSet); // [a, y]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.headSet("a")); // []
System.out.println(set.headSet("a", true)); // [a]

System.out.println(set.headSet("b")); // [a]
System.out.println(set.headSet("b", true)); // [a, b]

System.out.println(set.headSet("c")); // [a, b]
System.out.println(set.headSet("c", true)); // [a, b, c]

System.out.println(set.headSet("d")); // [a, b, c]
System.out.println(set.headSet("d", true)); // [a, b, c]

E higher (E e)

返回此集合中严格大于给定元素的最小元素,如果不存在这样的元素,则返回 null。 (机器翻译)

final var set = new TreeSet<String>();
set.add("b");
set.add("d");
set.add("f");

System.out.println(set); // [b, d, f]

System.out.println(set.higher("a")); // b
System.out.println(set.higher("b")); // d
System.out.println(set.higher("c")); // d
System.out.println(set.higher("d")); // f
System.out.println(set.higher("e")); // f
System.out.println(set.higher("f")); // null
System.out.println(set.higher("g")); // null

boolean isEmpty ()

如果此集合不包含任何元素,则返回 true。 (机器翻译)

final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(set.isEmpty()); // false

set.clear();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true

Iterator<E> iterator ()

按升序返回此集合中元素的迭代器。 (机器翻译)

final var set = new TreeSet<String>();
set.add("aaa");
set.add("bbb");
set.add("ccc");

System.out.println(set); // [aaa, bbb, ccc]

final var iterator = set.iterator();

System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(s -> {
    System.out.println(s);
});

// Result
// ↓
//-- forEachRemaining --
//aaa
//bbb
//ccc

E last ()

返回此集合中当前的最后一个(最高)元素。 (机器翻译)

final var set = new TreeSet<String>();
System.out.println(set); // []

set.add("b");

System.out.println(set); // [b]
System.out.println(set.last()); // b

set.add("a");

System.out.println(set); // [a, b]
System.out.println(set.last()); // b

set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(set.last()); // c
final var set = new TreeSet<String>();
System.out.println(set); // []

try {
    set.last();
} catch (NoSuchElementException e) {
    System.out.println(e);
}

// Result
// ↓
//java.util.NoSuchElementException

E lower (E e)

返回此集合中严格小于给定元素的最大元素,如果不存在这样的元素,则返回 null。 (机器翻译)

final var set = new TreeSet<String>();
set.add("b");
set.add("d");
set.add("f");

System.out.println(set); // [b, d, f]

System.out.println(set.lower("a")); // null
System.out.println(set.lower("b")); // null
System.out.println(set.lower("c")); // b
System.out.println(set.lower("d")); // b
System.out.println(set.lower("e")); // d
System.out.println(set.lower("f")); // d
System.out.println(set.lower("g")); // f

E pollFirst ()

检索并删除第一个(最低)元素,如果此集合为空则返回 null(可选操作)。 (机器翻译)

final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.pollFirst()); // a
System.out.println(set); // [b, c]

System.out.println(set.pollFirst()); // b
System.out.println(set); // [c]

System.out.println(set.pollFirst()); // c
System.out.println(set); // []

System.out.println(set.pollFirst()); // null

E pollLast ()

检索并删除最后一个(最高)元素,如果此集合为空则返回 null(可选操作)。 (机器翻译)

final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.pollLast()); // c
System.out.println(set); // [a, b]

System.out.println(set.pollLast()); // b
System.out.println(set); // [a]

System.out.println(set.pollLast()); // a
System.out.println(set); // []

System.out.println(set.pollLast()); // null

boolean remove (Object o)

如果存在,则从此集合中删除指定元素。 (机器翻译)

final var src = Set.of("a", "b", "c");

{
    final var set = new TreeSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("")); // false
    System.out.println(set); // [a, b, c]
}
{
    final var set = new TreeSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("a")); // true
    System.out.println(set); // [b, c]
}
{
    final var set = new TreeSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("b")); // true
    System.out.println(set); // [a, c]
}
{
    final var set = new TreeSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("X")); // false
    System.out.println(set); // [a, b, c]
}

int size ()

返回此集合中的元素数量(其基数)。 (机器翻译)

final var set1 = new TreeSet<String>();
System.out.println(set1); // []
System.out.println(set1.size()); // 0

final var set2 = new TreeSet<String>();
Collections.addAll(set2, "a", "b", "c");
System.out.println(set2); // [a, b, c]
System.out.println(set2.size()); // 3

Spliterator<E> spliterator ()

针对该集合中的元素创建一个后期绑定和快速失败的 Spliterator。 (机器翻译)

final var set = new TreeSet<String>();
set.add("aaa");
set.add("bbb");
set.add("ccc");

System.out.println(set); // [aaa, bbb, ccc]

final var spliterator = set.spliterator();

System.out.println("-- forEachRemaining --");
spliterator.forEachRemaining(s -> {
    System.out.println(s);
});

// Result
// ↓
//-- forEachRemaining --
//aaa
//bbb
//ccc

NavigableSet<E> subSet (E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

返回此集合中元素范围从 fromElement 到 toElement 的部分视图。 (机器翻译)

final var set = new TreeSet<String>();
set.add("x");
set.add("y");
set.add("z");

System.out.println(set); // [x, y, z]

final var subSet = set.subSet("a", true, "y", true);
System.out.println(subSet); // [x, y]

set.add("a");

System.out.println(set); // [a, x, y, z]
System.out.println(subSet); // [a, x, y]

subSet.remove("x");

System.out.println(set); // [a, y, z]
System.out.println(subSet); // [a, y]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.subSet("a", true, "c", true)); // [a, b, c]
System.out.println(set.subSet("a", true, "c", false)); // [a, b]
System.out.println(set.subSet("a", false, "c", true)); // [b, c]
System.out.println(set.subSet("a", false, "c", false)); // [b]

System.out.println(set.subSet("a", true, "a", false)); // []
System.out.println(set.subSet("a", true, "b", false)); // [a]
System.out.println(set.subSet("a", true, "c", false)); // [a, b]
System.out.println(set.subSet("a", true, "d", false)); // [a, b, c]

System.out.println(set.subSet("a", true, "d", false)); // [a, b, c]
System.out.println(set.subSet("b", true, "d", false)); // [b, c]
System.out.println(set.subSet("c", true, "d", false)); // [c]
System.out.println(set.subSet("d", true, "d", false)); // []

SortedSet<E> subSet (E fromElement, E toElement)

返回此集合的部分视图,其元素范围从 fromElement(包括)到 toElement(不包括)。 (机器翻译)

final var set = new TreeSet<String>();
set.add("x");
set.add("y");
set.add("z");

System.out.println(set); // [x, y, z]

final var subSet = set.subSet("a", "z");
System.out.println(subSet); // [x, y]

set.add("a");

System.out.println(set); // [a, x, y, z]
System.out.println(subSet); // [a, x, y]

subSet.remove("x");

System.out.println(set); // [a, y, z]
System.out.println(subSet); // [a, y]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.subSet("a", "a")); // []
System.out.println(set.subSet("a", "b")); // [a]
System.out.println(set.subSet("a", "c")); // [a, b]
System.out.println(set.subSet("a", "d")); // [a, b, c]

System.out.println(set.subSet("a", "d")); // [a, b, c]
System.out.println(set.subSet("b", "d")); // [b, c]
System.out.println(set.subSet("c", "d")); // [c]
System.out.println(set.subSet("d", "d")); // []

SortedSet<E> tailSet (E fromElement)

返回此集合中元素大于或等于 fromElement 的部分视图。 (机器翻译)

final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

final var tailSet = set.tailSet("b");
System.out.println(tailSet); // [b, c]

set.add("d");

System.out.println(set); // [a, b, c, d]
System.out.println(tailSet); // [b, c, d]

tailSet.remove("b");

System.out.println(set); // [a, c, d]
System.out.println(tailSet); // [c, d]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.tailSet("a")); // [a, b, c]
System.out.println(set.tailSet("b")); // [b, c]
System.out.println(set.tailSet("c")); // [c]
System.out.println(set.tailSet("d")); // []

NavigableSet<E> tailSet (E fromElement, boolean inclusive)

返回此集合中元素大于(或等于,如果 inclusive 为真)fromElement 的部分视图。 (机器翻译)

final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

final var tailSet = set.tailSet("a", false);
System.out.println(tailSet); // [b, c]

set.add("d");

System.out.println(set); // [a, b, c, d]
System.out.println(tailSet); // [b, c, d]

tailSet.remove("b");

System.out.println(set); // [a, c, d]
System.out.println(tailSet); // [c, d]
final var set = new TreeSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.tailSet("a")); // [a, b, c]
System.out.println(set.tailSet("a", false)); // [b, c]

System.out.println(set.tailSet("b")); // [b, c]
System.out.println(set.tailSet("b", false)); // [c]

System.out.println(set.tailSet("c")); // [c]
System.out.println(set.tailSet("c", false)); // []

Methods declared in AbstractSet

equals, hashCode, removeAll

请参阅下面的链接。

Methods declared in AbstractCollection

containsAll, retainAll, toArray, toArray, toString

请参阅下面的链接。

Methods declared in Collection

parallelStream, removeIf, stream, toArray

请参阅下面的链接。

Methods declared in Iterable

forEach

请参阅下面的链接。

Methods declared in NavigableSet

removeFirst, removeLast, reversed

请参阅下面的链接。

Methods declared in Set

containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray

请参阅下面的链接。

Methods declared in SortedSet

getFirst, getLast

请参阅下面的链接。


相关文章

To top of page