In this shot, we will discuss how to use the SortedMap.tailMap()
method in Java. This method is present in the SortedMap
interface inside the java.util
package.
The SortedMap.tailMap()
method obtains the part of the map whose keys are greater than or equal to a given SortedMap
key.
SortedMap.tailMap(key)
The SortedMap.tailMap()
method takes one parameter:
Key
: This method accepts a key
of SortedMap
.The SortedMap.tailMap()
method returns:
Map
: The part of the map whose keys are greater than or equal to the given key of SortedMap
.Let’s look at the code snippet below:
import java.util.*;class Main{public static void main(String[] args){SortedMap<Integer, String> s = new TreeMap<Integer,String>();s.put(2,"Welcome");s.put(12,"to");s.put(31,"the");s.put(18,"world");s.put(25,"of");s.put(36,"java");System.out.println("SortedMap mappings are: "+ s);System.out.println("Map from the key=25: " + s.tailMap(25));}}
Main
class.main
function.SortedMap
consisting of Integer
type keys and String
type values.SortedMap
.SortedMap
.SortedMap.tailMap()
method to obtain and display the returned map. The returned map will contain (key,value)
pairs of keys equal to or greater than the key
passed to the SortedMap
.In this way, we can use the SortedMap.tailMap()
method in Java.