码迷,mamicode.com
首页 > 其他好文 > 详细

Map的isEmpty()与==null的区别

时间:2017-09-30 20:54:26      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:判断   span   put   bsp   ==   内容   empty   str   关系   

  • isEmpty()方法判断Map是否有内容(即new分配空间后是否put键值对),若没有内容则true,否则false
  • == null是判断map是否为null(即是否new分配空间,和其中的键值对没关系),若没有内容则true,否则false

例如:

1 Map map = new HashMap<String ,String>();
2 System.out.println("判断map是否有内容:"+map.isEmpty());//返回true
3 System.out.println("判断map是否为null:"+map==null);//返回false
1  Map map = new HashMap<String ,String>();
2  map=null;
3  System.out.println("判断map是否为null:"+(map==null));//结果为true
4  System.out.println("判断map是否有内容:"+map.isEmpty());//NullPointerException
1 Map map = new HashMap<String ,String>();
2 map.put(null,null);
3 System.out.println("判断map是否为null:"+(map==null));//false
4 System.out.println("判断map是否有内容:"+map.isEmpty());//false

 

Map的isEmpty()与==null的区别

标签:判断   span   put   bsp   ==   内容   empty   str   关系   

原文地址:http://www.cnblogs.com/zhima-hu/p/7615921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!