TreeMap pollLastEntry() in Java

pollLastEntry(): This method is available in java.util.TreeMap class of Java.

Syntax:

Entry<K, V> java.util.TreeMap.pollLastEntry()

This method removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

Parameters: NA

Returns: the removed last entry of this map, or null if this map is empty.

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMappollLastEntry {
    public static void main(String[] args) {

        TreeMap<Integer, String> treeMap =
new TreeMap<Integer, String>();

        treeMap.put(2, "Hello");
        treeMap.put(11, "Java");
        treeMap.put(23, "Program");
        treeMap.put(6, "C++");
        treeMap.put(25, "Program");

        System.out.println(treeMap.pollLastEntry());

    }
}

Output:

25=Program


No comments:

Post a Comment