多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 如何检查`HashMap`是否为空? > 原文: [https://beginnersbook.com/2014/08/how-to-check-if-a-hashmap-is-empty-or-not/](https://beginnersbook.com/2014/08/how-to-check-if-a-hashmap-is-empty-or-not/) #### 描述 用于检查`HashMap`是否为空的程序。我们使用`HashMap`类的`isEmpty()`方法来执行此检查。 #### 程序 ```java import java.util.HashMap; class HashMapIsEmptyExample{ public static void main(String args[]) { // Create a HashMap HashMap<Integer, String> hmap = new HashMap<Integer, String>(); // Checking whether HashMap is empty or not /* isEmpty() method signature and description - * public boolean isEmpty(): Returns true if this map * contains no key-value mappings. */ System.out.println("Is HashMap Empty? "+hmap.isEmpty()); // Adding few elements hmap.put(11, "Jack"); hmap.put(22, "Rock"); hmap.put(33, "Rick"); hmap.put(44, "Smith"); hmap.put(55, "Will"); // Checking again System.out.println("Is HashMap Empty? "+hmap.isEmpty()); } } ``` **输出:** ```java Is HashMap Empty? true Is HashMap Empty? false ```