1. ObjectUtils 类
isEmpty(Object obj):
检查对象是否为空或 null。
boolean isEmpty = ObjectUtils.isEmpty(object);
2. StringUtils
类
isEmpty(CharSequence cs)
: 检查字符串是否为空或长度为 0。
String str = "";
boolean isEmpty = StringUtils.isEmpty(str); // false
isBlank(CharSequence cs)
: 检查字符串是否为空或仅包含空白字符。
String str = " ";
boolean isBlank = StringUtils.isBlank(str); // false
3. CollectionUtils 类
isEmpty(Collection<?> coll)
: 检查集合是否为空。
List<String> list = new ArrayList<>();
boolean isEmpty = CollectionUtils.isEmpty(list);
4. BeanUtils
类
copyProperties(Object dest, Object source)
: 将源对象的属性复制到目标对象中。
5. 依赖
ObjectUtils
类和 StringUtils
类:
- 导入
org.apache.commons.lang3.ObjectUtils
和org.apache.commons.lang3.StringUtils
。 - Maven 依赖:
-
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency>
CollectionUtils
类:- 导入
org.apache.commons.collections4.CollectionUtils
。 -
Maven 依赖:
-
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.4</version> </dependency>
BeanUtils
类:- 导入
org.apache.commons.beanutils.BeanUtils
。 -
Maven 依赖:
-
<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.4</version> </dependency>