Concept
What are sorted collections in the Java Collections framework?
ComputerScienceOne / Sorted Collections
"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);"
Related Ideas
- How does Java perform binary searches on arrays and lists?ComputerScienceOne · Sorted Collections
- How do sorted collections maintain element order?ComputerScienceOne · Sorting
- How do the Arrays and Collections classes sort elements?ComputerScienceOne · Sorting
- What sections and topics are listed for the Java Programming Language?ComputerScienceOne · Sorting Pointers to Elements . . . . . . . . . . . . . . . . . . . . .
- What does the section on handling null values cover?ComputerScienceOne · Sorted Collections . . . . . . . . . . . . . . . . . . . . . . . . . . .