Java HashMap está vacío ()

El método isEmpty () de Java HashMap comprueba si el mapa de hash está vacío.

La sintaxis del isEmpty()método es:

 hashmap.isEmpty()

Aquí, hashmap es un objeto de la HashMapclase.

Parámetros de isEmpty ()

El isEmpty()método no toma ningún parámetro.

isEmpty () Valor devuelto

  • devuelve truesi el mapa de hash no contiene ninguna asignación de clave / valor
  • devuelve falsesi el hashmap contiene asignaciones clave / valor

Ejemplo: comprobar si HashMap está vacío

 import java.util.HashMap; class Main ( public static void main(String() args) ( // create an HashMap HashMap languages = new HashMap(); System.out.println("Newly Created HashMap: " + languages); // checks if the HashMap has any element boolean result = languages.isEmpty(); // true System.out.println("Is the HashMap empty? " + result); // insert some elements to the HashMap languages.put("Python", 1); languages.put("Java", 14); System.out.println("Updated HashMap: " + languages); // checks if the HashMap is empty result = languages.isEmpty(); // false System.out.println("Is the HashMap empty? " + result); ) )

Salida

HashMap recién creado: () ¿Está vacío el HashMap? true HashMap actualizado: (Java = 14, Python = 1) ¿Está vacío el HashMap? falso

En el ejemplo anterior, hemos creado un mapa de hash llamado languages. Aquí, hemos utilizado el isEmpty()método para verificar si el mapa de hash contiene algún elemento o no.

Inicialmente, el mapa hash recién creado no contiene ningún elemento. Por lo tanto, isEmpty()vuelve true. Sin embargo, después de agregar algunos elementos ( Python , Java ), el método regresa false.

Articulos interesantes...