Java : SortedSet with Examples
SortedSet (Java SE 21 & JDK 21) with Examples.
You will find code examples on most SortedSet methods.
Summary
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)
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)
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 ()
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)
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 {
final var ret = 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 {
final var ret = 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 ()
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)
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)
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
Methods declared in Iterable
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
- API Examples