The program demonstrates how to use the subMap method of the TreeMap class to get a subset of keys and their corresponding values from a TreeMap.
import java.util.*; import java.util.Map.Entry; public class KeysRange { public static void main(String args[]) { TreeMap<Integer,String> s1 = new TreeMap<Integer,String>(); s1.put(101, "John"); s1.put(102, "Charles"); s1.put(103, "Joes"); s1.put(104, "George"); s1.put(105, "Smith"); SortedMap<Integer,String> s2 = new TreeMap<Integer,String>(); System.out.println("Given TreeMap : " + s1); s2 = s1.subMap(101, true, 103, true); System.out.println("Sub TreeMap Key Values : " + s2); } }
Given TreeMap : {101=John, 102=Charles, 103=Joes, 104=George, 105=Smith} Sub TreeMap Key Values : {101=John, 102=Charles, 103=Joes}
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions