In this shot, we will discuss how to use the SortedMap.subMap()
method in Java. This method is present in the SortedMap
interface inside the java.util
package.
The SortedMap.subMap()
method is used to obtain the part of the map for a given range of keys of SortedMap
.
The SortedMap.subMap()
method takes two parameters:
The SortedMap.subMap()
method returns:
SortedMap
.Let’s look at the below code snippet.
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 min_limit_key=18 to max_limit_key=36: " + s.subMap(18,36));}}
Main
class.main
function.SortedMap
consisting of Integer type keys and String type values.TreeMap.put()
method.SortedMap
.SortedMap.subMap()
method to obtain the subMap
for a range of keys and displayed it.In this way, we can use SortedMap.subMap()
method in Java.