English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
La méthode isEmpty() de HashMap Java vérifie si la carte de hachage est vide.
la syntaxe de la méthode isEmpty() est :
hashmap.isEmpty()
méthode isEmpty() sans paramètres.
si la carte de hachage ne contient aucuneclé/valeurretourne true si la carte de hachage est présente
si la carte de hachage contientclé/valeurretourne false si la carte est présente
import java.util.HashMap; class Main { public static void main(String[] args) { //créer HashMap HashMap<String, Integer> languages = new HashMap<>(); System.out.println("nouveau HashMap créé: " + languages); //vérifier si HashMap contient des éléments boolean result = languages.isEmpty(); // true System.out.println("Is HashMap empty? ", + result); //insérer quelques éléments dans HashMap languages.put("Python", 1);}} languages.put("Java", 14);}} System.out.println("Updated HashMap: ", + languages); //Check if HashMap is empty result = languages.isEmpty(); // false System.out.println("Is HashMap empty? ", + result); } }
Output result
Newly created HashMap: {} Is HashMap empty? true Updated HashMap: {Java=14, Python=1} Is HashMap empty? false
In the above example, we created a hash map named languages. Here, we used the isEmpty() method to check if the hash map contains any elements.
Initially, the newly created hash map does not contain any elements. Therefore, isEmpty() returns true. However, after adding some elements (Python,JavaAfter that, the method returns false.