This program demonstrates the use of pollLastEntry() method of TreeMap class to remove and get the last (highest) key-value pair from the TreeMap. Here's what the program does:
import java.util.*; import java.util.Map.Entry; public class RemoveGet { 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"); s1.put(106, "Abi"); System.out.println("Value Before poll : " + s1); System.out.println("Value Returned : " + s1.pollLastEntry()); System.out.println("Value After poll : " + s1); } }
Value Before poll : {101=John, 102=Charles, 103=Joes, 104=George, 105=Smith, 106=Abi} Value Returned : 106=Abi Value After poll : {101=John, 102=Charles, 103=Joes, 104=George, 105=Smith}
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions