java中如何判断map为空呢?

欣喜 Java经验 发布时间:2023-11-17 15:12:23 阅读数:15757 1
下文笔者讲述java代码判断map是否为空的方法及示例分享,如下所示

map是否为空的判断方法

判断map 是否为null
       我们可以使用  ==null 进行判断

判断map是否有内容
       我们可以使用 isEmpty()方法
例:map是否为空的判断方法
    Map map = new HashMap<String ,String>();
         System.out.println("判断map是否有内容:"+map.isEmpty());//返回true
         System.out.println("判断map是否为null:"+map==null);//返回false
  
  Map map = new HashMap<String ,String>();
  map=null;
  System.out.println("判断map是否为null:"+(map==null));//结果为true
  System.out.println("判断map是否有内容:"+map.isEmpty());//NullPointerException
  
  Map map = new HashMap<String ,String>();
  map.put(null,null);
  System.out.println("判断map是否为null:"+(map==null));//false
  System.out.println("判断map是否有内容:"+map.isEmpty());//false
 
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202311/17002051777327.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者