Open In App

NavigableMap firstEntry() method in Java

Last Updated : 29 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The firstEntry() method of NavigableMap interface in Java is used to return a key-value mapping associated with the least key in this map, or null if the map is empty.

Syntax:

Map.Entry< K, V > firstEntry()

Where, K is the type of key maintained by this map and V is the type of values mapped to the keys.

Parameters: It does not accepts any parameter.

Return Value: It returns a key-value mapping associated with the least key in this map, or null if the map is empty.

Below programs illustrate the firstEntry() method in Java:

Program 1: When the key is integer.




// Java code to demonstrate the working of
// firstEntry() method
  
import java.io.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the NavigableMap of Integer and String
        NavigableMap<Integer, String> nmmp = new TreeMap<>();
  
        // assigning the values in the NavigableMap
        // using put()
        nmmp.put(2, "two");
        nmmp.put(7, "seven");
        nmmp.put(3, "three");
  
        System.out.println("The mapping with least key is : "
                           + nmmp.firstEntry());
    }
}


Output:

The mapping with least key is : 2=two

Program 2: When the key is string.




// Java code to demonstrate the working of
// firstEntry() method
  
import java.io.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the NavigableMap of Integer and String
        NavigableMap<String, String> tmmp = new TreeMap<>();
  
        // assigning the values in the NavigableMap
        // using put()
        tmmp.put("one", "two");
        tmmp.put("six", "seven");
        tmmp.put("two", "three");
  
        System.out.println("The mapping associated with least key is : "
                           + tmmp.firstEntry());
    }
}


Output:

The mapping associated with least key is : one=two

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#firstEntry()



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads