Sorted collection in Java
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Overview of Sorted Collection in Java
In Java, working with collections often involves maintaining order, whether it's natural or custom-defined. The Sorted Collection framework facilitates the storage, retrieval, and ordering of elements, making operations like searching and traversal more efficient. Key interfaces like SortedSet and SortedMap are fundamental to how sorted collections operate.
SortedSet Interface
SortedSet is a direct extension of the Set interface and ensures that elements stored within are sorted according to their natural ordering or a specified comparator. TreeSet is an integral implementation of SortedSet in Java.
Key Features of SortedSet:
- Ordering: Elements are stored in a sorted sequence.
- Null Elements: Supports null elements if the natural ordering allows null.
- Comparator: The default is the natural ordering, but a custom
Comparatorcan be supplied. - End Points: Provides methods to retrieve the first and last elements efficiently.
Example Usage:
SortedMap Interface
SortedMap interface, another vital part of the Java Collections Framework, extends the Map interface. It holds key-value pairs sorted based on the keys either by their natural order or according to a custom comparator provided at map creation time. TreeMap is one of the most common implementations.
Key Features of SortedMap:
- Sorting: Keys are sorted.
- Navigable Functions: Provides methods to get a view of a portion of the map whose keys are strictly less than a given key, or greater than, etc.
- Null Values: Permit
nullvalues but generally does not allownullkeys (though, it's implementation dependent).
Example Usage:
Comparison between SortedSet and SortedMap
Sorted collections offer a way to manage ordered data efficiently, yet they cater to different needs. Below is a table summarizing key differences and features:
| Feature | SortedSet | SortedMap |
| Interface Extended | Set | Map |
| Purpose | Store sorted unique elements | Store key-value pairs with sorted keys |
| Common Implementation | TreeSet | TreeMap |
| Natural Ordering | Default for elements | Default for keys |
| Custom Comparator | Yes | Yes |
| Access Methods | first(), last() | firstKey(), lastKey() |
Additional Details and Considerations
- Comparators: Implementing custom comparators allows flexibility beyond natural order, enabling sorting based on derived attributes or a particular sequence.
- Performance: Both
TreeSetandTreeMaphave a time complexity of for basic operations like add, remove, and search. - Thread Safety: By default, sorted collections are not synchronized. For concurrent access from multiple threads, you can use
Collections.synchronizedSortedSetorCollections.synchronizedSortedMap.
Advanced Concepts
NavigableSet and NavigableMap
Java further enhances sorted collections with the NavigableSet and NavigableMap interfaces. These interfaces provide navigation methods such as:
floor(E e)- Finds greatest element less than or equal to a given element.ceiling(E e)- Finds least element greater than or equal to a given element.descendingSet()anddescendingMap()- Provides a reverse order view of the set/map.
Example of NavigableSet:
In conclusion, the Sorted Collections in Java provide robust and efficient tools to manage ordered data structures. Understanding their use and underlying mechanics is crucial for any Java developer aiming to write efficient, high-performance applications.
Related reading
- Sorted intervals query
- Sorting 100 unique numbers by using 40bytes of memory
- Sorting 10GB Data in 1 GB memory. How will I do it?
- Sorting 1 million 8-decimal-digit numbers with 1 MB of RAM
- Sorted Dictionary sorted on value in C LRU cache
- sorting a doubly linked list with merge sort
- Sorting a list with stream.sorted in Java
- Sorting algorithm of Arrays in Java.util package

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.