TreeMap lastEntry() in Java

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

Syntax:

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

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

Parameters: NA

Returns: an entry with the greatest key, or null if this map is empty.

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMaplastEntry {
    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.lastEntry());

    }
}

Output:

25=Program


No comments:

Post a Comment