En este tutorial aprenderemos cómo obtener las dimensiones de un TreeMap. Estamos usando método size () de la clase TreeMap para obtener el número de asignaciones de clave-valor para un archivo TreeMap. Aquí está el código completo:
Ejemplo
import java.util.TreeMap; class TreeMapSize { public static void main(String args[]) { // Create a TreeMap TreeMap<String, String> treemap = new TreeMap<String, String>(); // Put elements to the map treemap.put("Key1", "Value1"); treemap.put("Key2", "Value2"); treemap.put("Key3", "Value3"); treemap.put("Key4", "Value4"); treemap.put("Key5", "Value5"); // Displaying size of the TreeMap /* public int size(): Returns the number of key-value * mappings in this map. */ System.out.println("Size of TreeMap: "+treemap.size()); } }
Producción:
Size of TreeMap: 5