isEmpty的工具类
时间: 2023-07-03 22:30:10 浏览: 55
isEmpty是一个常用的工具类方法,用于判断字符串、集合、数组等对象是否为空。以下是一个示例代码:
```java
public class Utils {
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
public static <T> boolean isEmpty(Collection<T> collection) {
return collection == null || collection.isEmpty();
}
public static <T> boolean isEmpty(T[] array) {
return array == null || array.length == 0;
}
}
```
使用示例:
```java
String str = null;
if (Utils.isEmpty(str)) {
System.out.println("字符串为空");
}
List<String> list = new ArrayList<>();
if (Utils.isEmpty(list)) {
System.out.println("集合为空");
}
String[] array = new String[0];
if (Utils.isEmpty(array)) {
System.out.println("数组为空");
}
```
输出结果:
```
字符串为空
集合为空
数组为空
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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://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://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://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)