isEmpty()方法判断Map是否有内容(即new分配空间后是否put键值对),若没有内容则true,否则false
== null是判断map是否为null(即是否new分配空间,和其中的键值对没关系),若没有内容则true,否则false
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
博客介绍了Map中isEmpty()方法和== null判断的不同。isEmpty()用于判断Map在new分配空间后是否put了键值对,无内容则返回true;== null用于判断Map是否进行了new分配空间,未分配则返回true,与键值对无关。
2217

被折叠的 条评论
为什么被折叠?



