判断所有集合内部的元素是否为空,使用 isEmpty()方法,而不是 size()==0 的方式。
Map<String, Object> map = new HashMap<>();
if(map.isEmpty()) {
System.out.println("no element in this map.");
}
if (map.size() == 0){
System.out.println("no element in this map.");
}
前者的时间复杂度为 O(1),而且可读性更好。