Table of contents
1.
Introduction
2.
Hierarchy of Sorted Map
3.
Parameters and Declaration for SortedMap Class
4.
Constructor for SortedMap
5.
Operations Inherited from Map Interface
5.1.
put()
5.2.
remove()
5.3.
get()
5.4.
clear()
5.5.
size()
5.6.
keySet()
5.7.
values()
5.8.
Example
6.
Operations on SortedMap
6.1.
Comparator comparator( )
6.2.
SortedMap tailMap(Object start)
6.3.
Object lastKey( )
6.4.
SortedMap subMap(Object start, Object end)
6.5.
Object firstKey( )
6.6.
SortedMap headMap(Object end)
7.
Frequently asked questions
8.
Key Takeaway
Last Updated: Mar 27, 2024

SortedMap

Author Rhythm Jain
0 upvote

Introduction

SortedMap is an interface in Java that is a subinterface of the Map interface. The map entries are arranged in ascending order , sorted according to their keys.

It ensures that the entries are kept in ascending key order. It enables very efficient manipulation of map subsets.

Also read, Duck Number in Java and Hashcode Method in Java

Hierarchy of Sorted Map

The SortedMap interface is an extension of the Map interface. The SortedMap interface is implemented by the TreeMap and ConcurrentSkipListMap classes.
Given below is the hierarchy diagram:


Also see,  Swap Function in Java

Parameters and Declaration for SortedMap Class

Syntax For Declaration of SortedMap Class

interface SortedMap<K,V>
     extends Map<K,V>

SortedMap<K, V>

It takes two parameters, K and V, while construction, namely as follows:

K: The type of keys maintained by this map

V: The type of mapped values

Constructor for SortedMap

SortedMap is an interface, and objects cannot be created from an Interface. We always need a class or an extension of a class to make an object. Thus we can use the TreeMap class for object construction.

Syntax

SortedMap<K,V> set = new TreeMap<K, V> ();

Operations Inherited from Map Interface

First, let us discuss the operations that are inherited from the Map interface.

put()

put() method can add an element to the map. Also, we can change the element using the put() method.

remove()

We can use the remove() method to remove an element from the hashmap. This method takes the key as an input parameter and removes the mapping for a key from the given map if it exists.

get()

We can use the get() method to access an element from the hashmap for a corresponding key value.

clear()

We can use the clear() method to remove all the items from the Hashmap.

size()

we can use the size() method to get the total items present in the hashmap.

keySet()

We can use the keyset() method to iterate over the hashmap keys.

values()

We can use the values() method to iterate over the hashmap values.

Now let us demonstrate the above functions using an example.

Example

import java.io.*;
import java.util.*;


public class Main {


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


		//put()
		map1.put(1, "Ninja");
		map1.put(2, "Coder");
		map1.put(3, "Happy");
		map1.put(4, "Coding");
		System.out.println("Mappings of HashMap map1 are : "+ map1);

		//get()
		System.out.println("Value with key 2 is "+map1.get(2));

		//remove
		map1.remove(2);
		System.out.println("Mappings of HashMap map1 after removal are : "+ map1);


		//size
		System.out.println("Size of map1 are "+map1.size());


		//keyset()
		for (int i : map1.keySet()) {
            		System.out.println(i);
         		}
        		
            		//values()
		for (String i : map1.values()) {
            		System.out.println(i);
		}
        		
		//clear()
		map1.clear();
		System.out.println("Mappings of HashMap map1 after clear are : "+ map1);


	}
}
You can also try this code with Online Java Compiler
Run Code

Output

Mappings of HashMap map1 are: {1=Ninja, 2=Coder, 3=Happy, 4=Coding}
Value with key 2 is Coder
Mappings of HashMap map1 after removal are: {1=Ninja, 3=Happy, 4=Coding}
Size of map1 are 3
1
3
4
Ninja
Happy
Coding
Mappings of HashMap map1 after clear are: {}
You can also try this code with Online Java Compiler
Run Code


Try it on java online compiler.

Operations on SortedMap

The most commonly used operations of SortedMap are:

Comparator comparator( )

Returns the comparator of the invoking sorted map. If the sorted map is invoked using the default ordering, null is returned.

SortedMap tailMap(Object start)

Returns a map with items whose keys are larger than or equal to start.

Object lastKey( )

Returns the last key in the sorted map.

SortedMap subMap(Object start, Object end)

Returns a map with keys greater than or equal to start and less than the end.

Object firstKey( )

Returns the first key in the sorted map.

SortedMap headMap(Object end)

Returns a map with keys greater than or equal to start and less than the end.


Must Read: entryset java

Frequently asked questions

  1. Which class implements the SortedMap interface?
    The TreeMap class acts as an interface of the SortedMap interface.
     
  2. What will happen if we store duplicate values in the SortedMap?
    If we try to store a key already present in the HashMap, it will override the old value associated with the key and replace it with the new value.
     
  3. How can we iterate through the SortedMap?
    We can iterate through the for loop and get the keys. We can find the value of the key using the getValue() method.

Key Takeaway

Today, we learned about SortedMap in Java, its implementation, and its functionality and methods. We also learned how it is interfaced and how it is related to the map hierarchy.

If you want to learn about Hashmap in Java, you can visit our article Introduction to HashMap and, Internal Working of HashMap

Happy Coding!

Live masterclass