Java : SortedSet 示例

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

注解 :

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

简介

进一步提供其元素的全排序的集合。元素使用其自然顺序进行排序,或通过通常在创建有序集合时提供的比较器进行排序。集合的迭代器将按元素的升序遍历集合。 (机器翻译)

Class diagram

final SortedSet<String> sortedSet = new TreeSet<>();
sortedSet.add("ccc");
sortedSet.add("ddd");
sortedSet.add("aaa");
sortedSet.add("bbb");

System.out.println(sortedSet); // [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]

Methods

default void addFirst (E e)

抛出 UnsupportedOperationException。 (机器翻译)

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

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

// Result
// ↓
//UnsupportedOperationException!

default void addLast (E e)

抛出 UnsupportedOperationException。 (机器翻译)

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

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

// Result
// ↓
//UnsupportedOperationException!

Comparator<? super E> comparator ()

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

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

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

final var comparator = set.comparator();
System.out.println(Objects.equals(comparator, Comparator.reverseOrder())); // true
final SortedSet<String> 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

E first ()

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

final SortedSet<String> set = new TreeSet<>();
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 SortedSet<String> set = new TreeSet<>();
System.out.println(set); // []

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

// Result
// ↓
//NoSuchElementException!

default E getFirst ()

获取此集合的第一个元素。 (机器翻译)

This method is equivalent to first().

default E getLast ()

获取此集合的最后一个元素。 (机器翻译)

This method is equivalent to last().

SortedSet<E> headSet (E toElement)

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

final SortedSet<String> set = new TreeSet<>();
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 SortedSet<String> set = new TreeSet<>();
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]

E last ()

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

final SortedSet<String> set = new TreeSet<>();
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 SortedSet<String> set = new TreeSet<>();
System.out.println(set); // []

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

// Result
// ↓
//NoSuchElementException!

default E removeFirst ()

删除并返回此集合的第一个元素(可选操作)。 (机器翻译)

final SortedSet<String> set = new TreeSet<>();

set.add("aaa");
set.add("bbb");
set.add("ccc");

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

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

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

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

try {
    var _ = set.removeFirst();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

default E removeLast ()

删除并返回此集合的最后一个元素(可选操作)。 (机器翻译)

final SortedSet<String> set = new TreeSet<>();

set.add("aaa");
set.add("bbb");
set.add("ccc");

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

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

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

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

try {
    var _ = set.removeLast();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

default SortedSet<E> reversed ()

返回此集合的反向排序视图。 (机器翻译)

final SortedSet<String> set = new TreeSet<>();

set.add("aaa");
set.add("bbb");
set.add("ccc");

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

final var reversedSet = set.reversed();
System.out.println(reversedSet); // [ccc, bbb, aaa]

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

default Spliterator<E> spliterator ()

针对该有序集合中的元素创建一个 Spliterator。 (机器翻译)

final SortedSet<String> set = new TreeSet<>();
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

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

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

final SortedSet<String> set = new TreeSet<>();
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 SortedSet<String> set = new TreeSet<>();
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 SortedSet<String> set = new TreeSet<>();
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 SortedSet<String> set = new TreeSet<>();
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")); // []

Methods declared in Collection

parallelStream, removeIf, stream, toArray

请参阅下面的链接。

Methods declared in Iterable

forEach

请参阅下面的链接。

Methods declared in Set

add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray

请参阅下面的链接。


相关文章

To top of page