Java : SortedSet with Examples

SortedSet (Java SE 21 & JDK 21) with Examples.
You will find code examples on most SortedSet methods.


Summary

A Set that further provides a total ordering on its elements. The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time. The set's iterator will traverse the set in ascending element order.

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)

Throws 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)

Throws 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 ()

Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.

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 ()

Returns the first (lowest) element currently in this set.

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 ()

Gets the first element of this collection.

This method is equivalent to first().

default E getLast ()

Gets the last element of this collection.

This method is equivalent to last().

SortedSet<E> headSet (E toElement)

Returns a view of the portion of this set whose elements are strictly less than 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 ()

Returns the last (highest) element currently in this set.

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 ()

Removes and returns the first element of this collection (optional operation).

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 {
    final var ret = set.removeFirst();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

default E removeLast ()

Removes and returns the last element of this collection (optional operation).

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 {
    final var ret = set.removeLast();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

default SortedSet<E> reversed ()

Returns a reverse-ordered view of this collection.

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 ()

Creates a Spliterator over the elements in this sorted set.

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)

Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

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)

Returns a view of the portion of this set whose elements are greater than or equal to 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

Please see the link below.

Methods declared in Iterable

forEach

Please see the link below.

Methods declared in Set

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

Please see the link below.


Related posts

To top of page