Java SortedMap Interface

Java SortedMap Interface

Java SortedMap Interface

In this tutorial, we will learn about the Java SortedMap interface and its methods.

The SortedMap interface of the Java collections framework gives arranging of keys stored in a map.

It extends the Map interface.


The class that executes SortedMap

Since SortedMap is an interface, we can’t make objects from it.

So as to use the functionalities of the SortedMap interface, we have to use the class TreeMap that actualizes it.


How to use SortedMap?

To use the SortedMap, we must import the java.util.SortedMap package first. Once we import the package, here’s how we can create a sorted map.

// SortedMap implementation by TreeMap class
SortedMap<Key, Value> numbers = new TreeMap<>();

We have created a sorted map called numbers using the TreeMap class.

Here,

  • Key – a unique identifier used to associate each element (value) in a map
  • Value – elements associated by keys in a map

Here, we have used no contentions to make a sorted map. Henceforth the map will be arranged normally (ascending order).


Methods of SortedMap

The SortedMap interface incorporates all the strategies for the Map interface. It is on the grounds that Map is a super interface of SortedMap.

Other than every one of those techniques, here are the strategies explicit to the SortedMap interface.

  • comparator() – returns a comparator that can be used to order keys in a map
  • firstKey() – returns the first key of the sorted map
  • lastKey() – returns the last key of the sorted map
  • headMap(key) – returns all the entries of a map whose keys are less than the specified key
  • tailMap(key) – returns all the entries of a map whose keys are greater than or equal to the specified key
  • subMap(key1, key2) – returns all the entries of a map whose keys lies in between key1 and key2 including key1

To learn more, visit Java SortedMap (official Java documentation).


Implementation of SortedMap in TreeMap Class

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

class Main {

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

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


        // Access the first key of the map
        System.out.println("First Key: " + numbers.firstKey());

        // Access the last key of the map
        System.out.println("Last Key: " + numbers.lastKey());

        // Remove elements from the map
        int value = numbers.remove("One");
        System.out.println("Removed Value: " + value);
    }
}

Output

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

Now that we know about the SortedMap interface, we will learn about its implementation using the TreeMap class.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

Leave a Reply

Your email address will not be published. Required fields are marked *