hutool判断list是否为空
时间: 2023-08-07 09:31:48 浏览: 1014
![PDF](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PDF.png)
Java如何优雅地判断对象是否为空
Hutool 工具中提供了多种方式判断一个 List 是否为空:
1. 使用 `CollUtil.isEmpty(list)` 方法判断 List 是否为空,代码示例如下:
```java
import cn.hutool.core.collection.CollUtil;
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
boolean isEmpty = CollUtil.isEmpty(list);
System.out.println(isEmpty);
}
}
```
2. 使用 `ObjectUtil.isNull(list) || list.isEmpty()` 方法判断 List 是否为空,代码示例如下:
```java
import cn.hutool.core.util.ObjectUtil;
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
boolean isEmpty = ObjectUtil.isNull(list) || list.isEmpty();
System.out.println(isEmpty);
}
}
```
以上两种方式都可以判断 List 是否为空,具体使用哪种方式取决于个人习惯和项目中的具体情况。
希望能够帮助到您。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)