Método TreeMap firstEntry() en Java con ejemplos

TreeMap firstEntry() se refiere al método utilizado para recuperar los pares clave-valor asignados con el elemento de valor clave más bajo que existe en el TreeMap, si no hay pares clave-valor dentro del Mapa, simplemente devuelve nulo. El nombre del método ‘firstEntry()’ se explica por sí mismo que devolverá la entrada que tenga el elemento menos clave con su valor.

Syntax: public Map.Entry<K,V> firstEntry(), 

Here K and V refer key-value respectively.

Parameters: NA - As it doesnot accept any parameter.

Return Value: It returns an entry with the least key (lowest key-value pair) and null if the 
TreeMap is empty.

Exception: NA - As it doesnot throw any exception at entry return.

Ejemplo 1:

Java

// Java code to demonstrate 
// the working of TreeMap firstKey() 
import java.io.*; 
import java.util.*; 
public class treeMapFirstKey { 
public static void main(String[] args) 
    { 
    
        // Declaring the TreeMap of Key - Value Pairs
        // Integer - Key , String - Value
        TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); 
    
        // Adding-up the values in the TreeMap 
        // Use put() function to append data 
        treemap.put(2, "Java"); 
        treemap.put(4, "CPP"); 
        treemap.put(5, "PHP"); 
        treemap.put(1, "Python"); 
        treemap.put(3, "C"); 
    
        // Check for firstEntry()
        System.out.println("Lowest Entry is: " + treemap.firstEntry()); 
    } 
}
Producción

Lowest Entry is: 1=Python

Con el ejemplo anterior, está claro que firstEntry() comprueba comparar cada clave de TreeMap y devuelve el menor par clave-valor.

Ejemplo 2:

Java

// Java code to demonstrate the working
// of TreeMap firstKey() method
import java.io.*; 
import java.util.*; 
public class percentageFirstKey { 
public static void main(String[] args) 
    { 
    
        // Declaring the TreeMap of Key - Value Pairs
        // Double - Key , String - Value
        TreeMap<Double, String> treemapPercentage = new TreeMap<Double, String>(); 
    
        // Adding-up the values in the TreeMap 
        // Use put() function to append data 
        treemapPercentage.put(81.40, "Shashank"); 
        treemapPercentage.put(72.80, "Anand");
        treemapPercentage.put(65.50, "Gulshan");
        treemapPercentage.put(71.10, "Abhishek"); 
        treemapPercentage.put(70.20, "Ram"); 
    
        // Check for firstEntry()
        System.out.println("Lowest Entry is: " + treemapPercentage.firstEntry()); 
    } 
}
Producción

Lowest Entry is: 65.5=Gulshan

Algunos puntos en TreeMap firstEntry():

  • TreeMap firstEntry() está disponible en el paquete java.util.
  • No arroja una excepción en el momento de la entrada de retorno.
  • El método TreeMap firstEntry() es un método no estático ya que es accesible por el objeto de la clase, aparte de eso, obtendrá un error.

Publicación traducida automáticamente

Artículo escrito por night_fury1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *