Java : SortedSet con ejemplos
SortedSet (Java SE 23 & JDK 23) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos SortedSet<E>.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Un conjunto que además proporciona un ordenamiento total de sus elementos. Los elementos se ordenan utilizando su ordenamiento natural o mediante un comparador que normalmente se proporciona en el momento de creación del conjunto ordenado. El iterador del conjunto recorrerá el conjunto en orden ascendente de elementos. (Traducción automática)
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)
Lanza una excepción UnsupportedOperationException. (Traducción automática)
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)
Lanza una excepción UnsupportedOperationException. (Traducción automática)
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 ()
Devuelve el comparador utilizado para ordenar los elementos de este conjunto, o null si este conjunto utiliza el orden natural de sus elementos. (Traducción automática)
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 ()
Devuelve el primer elemento (más bajo) actualmente en este conjunto. (Traducción automática)
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 ()
Obtiene el primer elemento de esta colección. (Traducción automática)
This method is equivalent to first().
default E getLast ()
Obtiene el último elemento de esta colección. (Traducción automática)
This method is equivalent to last().
SortedSet<E> headSet (E toElement)
Devuelve una vista de la parte de este conjunto cuyos elementos son estrictamente menores que toElement. (Traducción automática)
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 ()
Devuelve el último elemento (más alto) actualmente en este conjunto. (Traducción automática)
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 ()
Elimina y devuelve el primer elemento de esta colección (operación opcional). (Traducción automática)
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 ()
Elimina y devuelve el último elemento de esta colección (operación opcional). (Traducción automática)
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 ()
Devuelve una vista ordenada invertida de esta colección. (Traducción automática)
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 ()
Crea un divisor sobre los elementos de este conjunto ordenado. (Traducción automática)
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)
Devuelve una vista de la parte de este conjunto cuyos elementos van desde fromElement, inclusive, hasta toElement, exclusivo. (Traducción automática)
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)
Devuelve una vista de la parte de este conjunto cuyos elementos son mayores o iguales a fromElement. (Traducción automática)
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
Consulte el siguiente enlace.
Related posts
- Ejemplos de API