java中如何判断map为空呢?
下文笔者讲述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
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


