1.String字符串
String a = ...;
if (a == null || a.isEmpty()) {
// a 为空或长度为 0
System.out.println("a 为空或长度为 0");
}
2.Object对象
Object dym=...;
if(dym==null){
// dym 为空
System.out.println("dym 为空");
}
3.List<String> 集合
List<String> schtEntryIds
if (schtEntryIds == null || schtEntryIds.isEmpty()) {
// schtEntryIds 为空或没有元素
System.out.println("schtEntryIds 为空或没有元素");
}
4.Map<String,String>集合
Map<String, String> myMap = ...; // 初始化您的 Map
if (myMap == null || myMap.isEmpty()) {
// myMap 为空或没有键值对
System.out.println("myMap 为空或没有键值对");
}
5.说明
null 检查:首先检查 schtEntryIds 是否为 null,以避免在调用 isEmpty() 方法时抛出 NullPointerException。
isEmpty() 方法:如果 schtEntryIds 不为 null,则使用 isEmpty() 方法检查列表是否包含任何元素。
6.总结:
适用的集合类型
List(如 ArrayList、LinkedList)
Set(如 HashSet、TreeSet)
Map(如 HashMap、TreeMap,使用 map.isEmpty())
Queue(如 LinkedList、PriorityQueue)
Deque(如 ArrayDeque、LinkedList)
Collection(所有集合的父接口)
对于所有这些集合类型,判断是否为空的通用方法是:
(1). 检查集合是否为 null。
(2). 使用 isEmpty() 方法检查集合是否包含任何元素。