Método TreeMap headMap() en Java

El método java.util.TreeMap.headMap( key_point ) de la clase TreeMap se usa para obtener todos los pares o partes del mapa estrictamente menos que el parámetro key_value. El parámetro mencionado se excluye del diagrama de árbol recién preparado. Dado que el conjunto está respaldado por el mapa, cualquier cambio en el mapa se refleja en el otro mapa y viceversa.

Sintaxis:

sorted_map = old_treemap.headMap(key_point)

Parámetros: El método toma un parámetro key_point del tipo de clave tomada en el TreeMap y hace referencia al punto, hasta el cual se devolverá el par clave-valor.

Valor devuelto: el método devuelve la parte del mapa de árbol cuyas claves son estrictamente menores que las de key_point.

Excepciones: el método arroja tres tipos de excepciones:

  • ClassCastException: esta excepción se lanza cuando key_point no es compatible o comparable con el comparador de mapas.
  • NullPointerException: esta excepción se produce cuando el punto clave es nulo.
  • IllegalArgumentException: esta excepción se lanza cuando key_point está fuera de límite o fuera del límite del rango del mapa.

Los siguientes programas ilustran el uso del método java.util.TreeMap.headMap():
Programa 1:

// Java code to illustrate the get() method
import java.util.*;
  
public class Tree_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty TreeMap
        TreeMap<Integer, String> tree_map = new 
                     TreeMap<Integer, String>();
  
        // Mapping string values to int keys
        tree_map.put(10, "Geeks");
        tree_map.put(15, "4");
        tree_map.put(20, "Geeks");
        tree_map.put(25, "Welcomes");
        tree_map.put(30, "You");
  
        // Displaying the TreeMap
        System.out.println("Initial Tree is: " + 
                                       tree_map);
  
        // Creating the sorted map for map head
        SortedMap<Integer, String> map_head = new 
                       TreeMap<Integer, String>();
        map_head = tree_map.headMap(20);
  
        // Getting the map head
        System.out.println("The headmap is: " + map_head);
    }
}
Producción:

Initial Tree is: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
The headmap is: {10=Geeks, 15=4}

Programa 2:

// Java code to illustrate the get() method
import java.util.*;
  
public class Tree_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty TreeMap
        TreeMap<String, Integer> tree_map = new 
                      TreeMap<String, Integer>();
  
        // Mapping int values to string keys
        tree_map.put("Geeks", 10);
        tree_map.put("4", 15);
        tree_map.put("Geeks", 20);
        tree_map.put("Welcomes", 25);
        tree_map.put("You", 30);
  
        // Displaying the TreeMap
        System.out.println("Initial Tree is: " + 
                                       tree_map);
  
        // Creating the sorted map for map head
        SortedMap<String, Integer> map_head = new 
                       TreeMap<String, Integer>();
        map_head = tree_map.headMap("You");
  
        // Getting the map head
        System.out.println("The headmap is: " +
                                      map_head);
    }
}
Producción:

Initial Tree is: {4=15, Geeks=20, Welcomes=25, You=30}
The headmap is: {4=15, Geeks=20, Welcomes=25}

Nota: La misma operación se puede realizar con cualquier tipo de Mapping con variación y combinación de diferentes tipos de datos.

Publicación traducida automáticamente

Artículo escrito por Chinmoy Lenka 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 *