TreeMap firstEntry() in Java

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

Syntax:

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

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

Parameters: NA

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

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMapfirstEntry {
    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.firstEntry());
    }
}

Output:

2=Hello


No comments:

Post a Comment