怎么判断map不为空

public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        System.out.println("map为空:" + map.isEmpty());
        //加入元素
        map.put("1", "1");
        System.out.println("map为空:" + map.isEmpty());
    }
这里是用isEmpty()方法来做判断,其实和map.size()也没差,可以看下isEmpty()方法的源码:

 public boolean isEmpty() {
        return size == 0;
    }
1
2
3
其实就是把map的size和0做个判断,返回false和true结果。
map为空的问题
Map<String, Object> resultMap = new HashMap<>();

resultMap!=null

 

 

posted @ 2020-07-29 13:34  红尘沙漏  阅读(15278)  评论(0编辑  收藏  举报