Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java SortedMap Interface: Overview, Methods, and TreeMap Implementation

Java SortedMap Interface

In this tutorial, we explore the Java SortedMap interface, its key methods, and how to use it with the TreeMap class.

The Java SortedMap interface, part of the collections framework, guarantees that map keys are stored in a sorted order. It extends the base Map interface.

Java SortedMap Interface: Overview, Methods, and TreeMap Implementation


Class that Implements SortedMap

Because SortedMap is an interface, you cannot instantiate it directly. The concrete implementation provided by the JDK is the TreeMap class, which maintains entries in ascending key order by default.

Java SortedMap Interface: Overview, Methods, and TreeMap Implementation


How to Use SortedMap

To work with SortedMap, first import the interface:

import java.util.SortedMap;
import java.util.TreeMap;

Then create an instance using TreeMap:

SortedMap<Key, Value> numbers = new TreeMap<>();

In this example, numbers will store keys in natural ascending order. You can also supply a custom Comparator if you need a different ordering.

Definitions:

Using the no‑argument constructor means the map will be sorted according to the natural ordering of the key type.


Key Methods of SortedMap

Besides all methods inherited from Map, SortedMap adds the following operations:

For deeper details, refer to the Java SortedMap documentation.


TreeMap: Concrete Implementation of SortedMap

import java.util.SortedMap;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        // Creating a SortedMap using TreeMap
        SortedMap<String, Integer> numbers = new TreeMap<>();

        // Insert elements into the map
        numbers.put("Two", 2);
        numbers.put("One", 1);
        System.out.println("SortedMap: " + numbers);

        // Retrieve the first and last keys
        System.out.println("First Key: " + numbers.firstKey());
        System.out.println("Last Key: " + numbers.lastKey());

        // Remove an entry by key
        int removed = numbers.remove("One");
        System.out.println("Removed Value: " + removed);
    }
}

Output

SortedMap: {One=1, Two=2}
First Key: One
Last Key: Two
Removed Value: 1

This example demonstrates how SortedMap behaves when used with TreeMap. For more on TreeMap, see the Java TreeMap documentation.

Java

  1. Mastering Java Interfaces: Concepts, Implementation, and Best Practices
  2. Java Collections Framework: Core Interfaces, Implementations, and Practical Usage
  3. Java Collection Interface: Core Concepts & Essential Methods
  4. Mastering Java’s Queue Interface: Methods, Implementations, and Practical Use
  5. Mastering Java's Deque Interface: Features, Methods, and Practical Examples
  6. Java Map Interface – Comprehensive Guide to Map, Its Implementations, and Key Methods
  7. Mastering Java NavigableMap: Features, Methods, and TreeMap Implementation
  8. Mastering Java’s ConcurrentMap: Thread‑Safe Maps Explained
  9. Mastering Java’s Set Interface: Concepts, Methods, and Practical Examples
  10. Mastering Java SortedSet: A Practical Guide to TreeSet and Its Methods