Java : NavigableSet with Examples

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


Summary

A SortedSet extended with navigation methods reporting closest matches for given search targets. Methods lower(E), floor(E), ceiling(E), and higher(E) return elements respectively less than, less than or equal, greater than or equal, and greater than a given element, returning null if there is no such element.

Class diagram

final NavigableSet<String> set = new TreeSet<>();
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.lower("a")); // null
System.out.println(set.lower("b")); // a
System.out.println(set.lower("c")); // b

System.out.println(set.higher("a")); // b
System.out.println(set.higher("b")); // c
System.out.println(set.higher("c")); // null

Methods

E ceiling (E e)

Returns the least element in this set greater than or equal to the given element, or null if there is no such element.

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

Iterator<E> descendingIterator ()

Returns an iterator over the elements in this set, in descending order.

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

Returns a reverse order view of the elements contained in this set.

final NavigableSet<String> set = new TreeSet<>();
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 floor (E e)

Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.

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

Returns a view of the portion of this set whose elements are strictly less than toElement.

final NavigableSet<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 NavigableSet<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]

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

Returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.

final NavigableSet<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("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 NavigableSet<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("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)

Returns the least element in this set strictly greater than the given element, or null if there is no such element.

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

Iterator<E> iterator ()

Returns an iterator over the elements in this set, in ascending order.

final NavigableSet<String> set = new TreeSet<>();
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 lower (E e)

Returns the greatest element in this set strictly less than the given element, or null if there is no such element.

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

Retrieves and removes the first (lowest) element, or returns null if this set is empty.

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

Retrieves and removes the last (highest) element, or returns null if this set is empty.

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

default E removeFirst ()

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

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

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

// Result
// ↓
//NoSuchElementException!

default NavigableSet<E> reversed ()

Returns a reverse-ordered view of this collection.

final NavigableSet<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]

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

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

final NavigableSet<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", 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 NavigableSet<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", 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)

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

final NavigableSet<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 NavigableSet<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 NavigableSet<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 NavigableSet<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")); // []

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

Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement.

final NavigableSet<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("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 NavigableSet<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("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 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, remove, removeAll, retainAll, size, toArray, toArray

Please see the link below.

Methods declared in SortedSet

addFirst, addLast, comparator, first, getFirst, getLast, last, spliterator

Please see the link below.


Related posts

To top of page