SortedMap in Java is an interface that is a subinterface of the map interface. The data on the card is sorted in ascending order of their keys.

Simply put, this ensures that the documents are stored in ascending order of keys. This allows a very efficient treatment of the board’s subsets.

SortedMap has been added in version 1.2 of Java. It is included in the java.util.SortedMap package.

SortedMap interface hierarchy in Java

The SortedMap interface expands the map interface. The TreeMap and ConcurrentSkipListMap classes implement the SortedMap interface. The following figure shows a hierarchical scheme of the SortedMap interface in Java.

Sorted card interface

SortedMap is a generic interface that is declared as shown below:

interface Sorted card<K,V>
expands card<K,V>

Where K is the type of key supported by this card and V is the type of values assigned.

Grading methods in Java

In addition to the methods offered by the map interface, SortedMap offers several other methods, which are listed below:

1. Compare-Compare() : This method returns the comparator used to order the keys on this card. If the card uses the natural order of the keys, the zero is returned.

2. Setting<Map.Entry<K,V>> entrySet() : This method displays the record type of the key value pairs (mappings) in this card.

3. Set KeySet(): Gives an overview of the amount of keys in this map.

4. K firstKey() : Returns the first (smallest or lowest) key on the business card.

5. K lastKey() : Returns the last (highest or largest) key on the business card.

6. SortedMap<K,V> headMap(K toKey) : This method returns the part of the card whose key is strictly inferior to ToKey.

7. SortedMap<K,V> tailMap(K fromKey) : This method returns the part of the card whose key is greater than or equal to Key.

8. Sorted map <K,V> subcard (K to Key, K to Key) : This method returns part of the card whose keys are from Kay, inclusive, to Kay, exclusive.

Inherited methods of the map interface

clear(), containsKey(), containsValue(), equals(), get(), hashCode(), isEmpty(), put(), putAll(), remove(), size()

Some methods throw an exception called NoSuchElementException if there is no data on the business card. If an object is not compatible with the chart elements, a ClassCastException is launched.

If you try to use a null object when the map’s null object is not resolved, a runtime exception called NullPointerException is thrown.

Examples of Java SortedMap programs

Let’s take a look at examples of programs that perform different operations based on methods defined by the SortedMap interface in Java.

Since Java SortedMap is an interface, it can only be used with a class that implements the SortedMap interface. The TreeMap class in Java implements the SortedMap interface.

1. Adding and deleting items : Let’s create a program in which we add and remove one item at a time from the sorted map using the put() and remove() methods. However, the order of insertion is not defined in the TreeMap.

Within each element, the keys are compared and sorted in ascending order. Please refer to the following source code for a better understanding.

Program source code 1 :

Import java.util.SortedMap ;
Import java.util.TreeMap ;

public class SortedMapEx [
public static void head(string[] args)
[
// Create SortedMap.
SortedMap<Integer, String> smap = new TreeMap<>() ;

// Add elements to the tree map.
smap.put(10, 10);
smap.put(20, 20);
smap.put(05, 5);
smap.put(07, 7);
smap.put(40, 40) ;

System.out.println(card records: +smap) ;

// Erase the record.
Object removeEntry = smap.remove(05);
System.out.println(Removed record: +removeEntry);
System.out.println(Update records in the card after the removal operation: +smap) ;

}
}

Get out of here:
The entries are shown on the card: {5=five, 7=same, 10=ten, 20=twenty, 40=forty}
Entries deleted : Five
records updated on the map after the deletion process: 7=seven, 10=ten, 20=twenty, 40=forty}.

2. Updating files : If you want to update a record after adding records, you can do so by using the put() method to add the record again.

Since the records in SortedMap are indexed per key, the value of the key can be updated or changed by simply entering the current value of the key you want to change.

Program source code 2 :

Import java.util.SortedMap ;
Import java.util.TreeMap ;

public class SortedMapEx2 [
public static void head(string[] args)
[
// Create SortedMap.
SortedMap<Integer, String> smap = new TreeMap<>() ;

smap.put(06, Six);
smap.put(20, Twenty);
smap.put(05, FIE); // The entry here is incorrect.
smap.put(07, Seven);
smap.put(40, Forty) ;

System.out.println(Card entries: +smap.entrySet() ;

// Adjust the spelling of an invalid entry.
smap.put(05, 5);
System.out.println(Updated map entries: +smap.entrySet());
System.out.println(SubMap: +smap.subMap(10, 45));
}
}

Get out of here:
The entrances are on the card: [5=seat, 6=six, 7=seven, 20=hifty, 40=forty] Updated card entries: 5=Five, 6=Continuous, 7=Equal, 20=Twenty, 40=Forty] Subcard: {20=Twenty, 40=Forty}.

3. Let’s take another example of a program that performs different operations based on the headMap(), tailMap(), firstKey() and lastKey() methods. Please refer to the following source code for a better understanding.

Program source code 3 :

Import java.util.SortedMap ;
Import java.util.TreeMap ;

public class SortedMapEx3 [
public static void head(string[] args)
[
// Create SortedMap.
SortedMap<Integer, String> smap = new TreeMap<Integer, String>() ;

// Add elements to the tree map.
smap.put(90, John);
smap.put(85, Deep);
smap.put(100, Sophia);
smap.put(35, Olivea);
smap.put(39, Shubh) ;

System.out.println(Math marks: +smap.headMap(40));
System.out.println(Math marks: +smap.tailMap(40));
System.out.println(Math marks: +smap.headMap(40)) ;

System.out.println(Low notes: +smap.firstKey());
System.out.println(High notes: +smap.lastKey());
}
}

Get out of here:
Plates in math: {35=Oliva, 39=Shubh, 85=Depth, 90=John, 100=Sophia}
students who have passed the math exam: {85=Deep, 90=John, 100=Sophia}
students who failed the math exam: {35=Oliva, 39=Shubh}
Lowest: 35Highest scores: 100

I hope this tutorial has covered the important points related to the SortedMap interface in Java with sample programs. I hope you understand that.
Thanks for reading!

You May Also Like

How To Fix Thumbnails Not Showing In Windows 10

The Windows thumbnail function makes it easy to recognize photos and movies…

A Complete Guide On Resolving The Issue of Phone Overheating

The phone can sometimes even get hot, whether it’s during a game…

How To Fix Windows 10 File Explorer Dark Theme Not Working

When it comes to customisation options, Windows 10 offers users a considerable…

Apex Legends Crashing PC Fix 2020

Various problems can occur during gambling. If you’re playing the new royal…