This Java program demonstrates the usage of the headMap() method of the TreeMap class to get the entries whose keys are less than a specified key. Here is how the program works:
For example, if we call s1.headMap(103, true) , the program retrieves a view of the entries whose keys are less than or equal to 103, and includes the entry with the key 103.
import java.util.*; import java.util.Map.Entry; public class GetKey { 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"); System.out.println("Given TreeMap : " + s1); System.out.println("Checking the 101 Key(s) : " + s1.headMap(101, true)); System.out.println("Checking the 103 Key(s) : " + s1.headMap(103, true)); System.out.println("Checking the 107 Key(s) : " + s1.headMap(107, true)); } }
Given TreeMap : {101=John, 102=Charles, 103=Joes, 104=George, 105=Smith} Checking the 101 Key(s) : {101=John} Checking the 103 Key(s) : {101=John, 102=Charles, 103=Joes} Checking the 107 Key(s) : {101=John, 102=Charles, 103=Joes, 104=George, 105=Smith}
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions