Bifalgorithm post · Computer science one · Sorted Collections

What are sorted collections in the Java Collections framework?

What are sorted collections in the Java Collections framework? (Computer science one · Sorted Collections) — "The Java Collections framework also provides seve…

Ideas in this flowchart

  • Las estructuras de datos ordenadas del framework de colecciones de Java
  • Una estructura ordenada mantiene sus elementos en orden automáticamente
  • Una List puede ordenarse posteriormente mediante Collections.sort()
  • Los nuevos elementos se insertan en la posición apropiada
  • La estructura impide insertar elementos arbitrariamente en cualquier orden
  • SortedSet<T> es la interfaz de Java para colecciones ordenadas
  • TreeSet<T> es una implementación de SortedSet<T>
  • TreeSet<T> utiliza un árbol de búsqueda binaria equilibrado, específicamente un árbol rojo-negro
  • La forma de ordenar los elementos puede configurarse al construir TreeSet<T>
  • Se puede proporcionar un Comparator<T> para mantener el orden
  • Si se omite el Comparator, se utiliza el orden natural de los elementos
  • Ejemplo de creación y uso de un TreeSet<Integer>
  • El Comparator<Integer> cmpIntDesc define un orden descendente
  • sortedInts se construye con TreeSet<Integer>(cmpIntDesc)
  • Se agregan los valores 10, 30 y 20 al conjunto ordenado
Feed/

What are sorted collections in the Java Collections framework?

No cover

Channel

Computer science one

1 page

"The Java Collections framework also provides several sorted data structures. Though an ordered collection such as a List can be sorted calling the Collections.sort() method, a sorted data structure maintains an ordering. As you add elements to the data structure, they are inserted in the appropriate spot. Such data structures also prevent you from arbitrarily inserting elements in any order. Java defines the SortedSet<T> interface for sorted collections with a TreeSet<T> implementation. As the name implies, the implementation is a balanced binary search tree, in particular a red-black tree. You can construct a TreeSet<T> by providing it a Comparator<T> to use to maintain the ordering (alternatively, you may omit the Comparator and the TreeSet will use the natural ordering of elements).\n\nComparator<Integer> cmpIntDesc = new Comparator<Integer>() { ... };\n\nSortedSet<Integer> sortedInts = new TreeSet<Integer>(cmpIntDesc);\n\nsortedInts.add(10);\n\nsortedInts.add(30);\n\nsortedInts.add(20);"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.