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
-
Which class implements the SortedMap interface?
The TreeMap class acts as an interface of the SortedMap interface.
-
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.
-
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!