Java Map isEmpty()方法及实例

Java Map isEmpty()方法及实例

该方法用于检查一个Map是否有任何键和值对的条目。如果没有映射存在,则返回true。

语法

boolean isEmpty()

参数: 该方法没有参数。

返回: 如果Map不包含任何键值映射,该方法返回True。

下面的程序展示了int isEmpty()方法的实现。

程序1:

// Java code to show the implementation of
// isEmpty method in Map interface
import java.util.*;
public class GfG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Initializing a Map of type HashMap
        Map<String, String> map = new HashMap<>();
  
        System.out.println(map);
  
        System.out.println(map.isEmpty());
    }
}

输出。

{}
true

程序2: 下面的代码显示了hashCode()的实现。

// Java code to show the implementation of
// isEmpty method in Map interface
import java.util.*;
public class GfG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Initializing a Map of type HashMap
        Map<String, String> map = new HashMap<>();
        map.put("1", "One");
        map.put("3", "Three");
        map.put("5", "Five");
        map.put("7", "Seven");
        map.put("9", "Ninde");
        System.out.println(map);
  
        System.out.println(map.isEmpty());
    }
}

输出。

{1=One, 3=Three, 5=Five, 7=Seven, 9=Ninde}
false

参考资料:
Oracle文档

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程