ArrayList判断空和null的工具类
时间: 2023-12-10 15:42:34 浏览: 40
以下是一个简单的 ArrayList 判断空和 null 的工具类示例:
```java
import java.util.ArrayList;
public class ArrayListUtils {
public static boolean isEmpty(ArrayList<?> arrayList) {
return arrayList == null || arrayList.isEmpty();
}
public static boolean isNotEmpty(ArrayList<?> arrayList) {
return !isEmpty(arrayList);
}
}
```
使用方法如下:
```java
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
if (ArrayListUtils.isNotEmpty(list)) {
System.out.println("List is not empty");
}
list = null;
if (ArrayListUtils.isEmpty(list)) {
System.out.println("List is empty or null");
}
```
输出结果:
```
List is not empty
List is empty or null
```
相关问题
lang3包判断集合是否为null和空的方法
Lang3包是Apache Commons项目的一部分,提供了一些常用的工具类和方法。在Lang3包中,判断集合是否为null和空的方法有以下几种:
1. 使用CollectionUtils.isEmpty()方法判断集合是否为空。该方法返回一个布尔值,如果集合为null或者集合中没有元素,则返回 true,否则返回 false。例如:
```java
List<String> list = new ArrayList<>();
if (CollectionUtils.isEmpty(list)) {
// 集合为空
}
```
2. 使用CollectionUtils.isNotEmpty()方法判断集合是否为空。该方法返回一个布尔值,如果集合不为null且集合中有元素,则返回 true,否则返回 false。例如:
```java
List<String> list = new ArrayList<>();
if (CollectionUtils.isNotEmpty(list)) {
// 集合不为空
}
```
3. 使用ArrayUtils.isEmpty()方法判断数组是否为空。该方法返回一个布尔值,如果数组为null或者数组中没有元素,则返回 true,否则返回 false。例如:
```java
String[] array = new String[0];
if (ArrayUtils.isEmpty(array)) {
// 数组为空
}
```
4. 使用ArrayUtils.isNotEmpty()方法判断数组是否为空。该方法返回一个布尔值,如果数组不为null且数组中有元素,则返回 true,否则返回 false。例如:
```java
String[] array = new String[0];
if (ArrayUtils.isNotEmpty(array)) {
// 数组不为空
}
```
以上是Lang3包中常用的判断集合是否为null和空的方法,使用时可以根据具体情况选择合适的方式。
Java工具类(四) 判断数组/集合为空的工具类
以下是一个Java工具类,可以用于判断数组、List、Set等集合类型是否为空:
```java
public class CollectionUtils {
/**
* 判断数组是否为空
* @param array 数组对象
* @return true:数组为空,false:数组不为空
*/
public static boolean isEmpty(Object[] array) {
return array == null || array.length == 0;
}
/**
* 判断List集合是否为空
* @param list List集合对象
* @return true:List集合为空,false:List集合不为空
*/
public static boolean isEmpty(List<?> list) {
return list == null || list.isEmpty();
}
/**
* 判断Set集合是否为空
* @param set Set集合对象
* @return true:Set集合为空,false:Set集合不为空
*/
public static boolean isEmpty(Set<?> set) {
return set == null || set.isEmpty();
}
/**
* 判断Map集合是否为空
* @param map Map集合对象
* @return true:Map集合为空,false:Map集合不为空
*/
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}
}
```
使用方法:
```java
String[] arr = new String[]{"a", "b", "c"};
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
Map<String, String> map = new HashMap<>();
System.out.println(CollectionUtils.isEmpty(arr)); // false
System.out.println(CollectionUtils.isEmpty(list)); // true
System.out.println(CollectionUtils.isEmpty(set)); // true
System.out.println(CollectionUtils.isEmpty(map)); // true
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)