常用工具类ObjectUtils

作者简介:码上言

代表教程:Spring Boot + vue-element 开发个人博客项目实战教程

专栏内容:个人博客系统

我的文档网站:http://xyhwh-nav.cn/

文章目录

常用工具类ObjectUtils

引入包

java 复制代码
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

常用方法

  1. isEmpty(Object obj)

检查对象是否为空。如果对象为null或者为空字符串、空数组、空Collection、空Map或者空Iterator,则返回true;否则返回false。

java 复制代码
String str = "";
boolean empty = ObjectUtils.isEmpty(str); // true
  1. isNotEmpty(Object obj)

检查对象是否非空。如果对象不为null且不为空字符串、空数组、空Collection、空Map或者空Iterator,则返回true;否则返回false。

java 复制代码
String str = "Hello";
boolean notEmpty = ObjectUtils.isNotEmpty(str); // true
  1. equals(Object object1, Object object2)

比较两个对象是否相等,可以处理null值,避免了NullPointerException的出现。

java 复制代码
String str1 = "Hello";
String str2 = "Hello";
boolean equals = ObjectUtils.equals(str1, str2); // true
  1. hashCode(Object obj)

计算对象的哈希码,可以处理null值。

java 复制代码
String str = "Hello";
int hashCode = ObjectUtils.hashCode(str); // 69609650
  1. toString(Object obj)

将对象转换为字符串。如果对象为空,则返回字符串"null"。

java 复制代码
int num = 123;
String str = ObjectUtils.toString(num); // "123"
  1. defaultIfNull(T object, T defaultValue)

如果对象为空,则返回默认值。

java 复制代码
String str = null;
String defaultStr = "default";
String result = ObjectUtils.defaultIfNull(str, defaultStr); // "default"
  1. allNotNull(Object... objects)

检查多个对象是否都不为空。如果所有对象都不为空,则返回true;否则返回false。

java 复制代码
String str1 = "Hello";
String str2 = "World";
boolean allNotNull = ObjectUtils.allNotNull(str1, str2); // true
  1. anyNotNull(Object... objects)

检查多个对象中是否至少有一个不为空。如果至少有一个对象不为空,则返回true;否则返回false。

java 复制代码
String str1 = "Hello";
String str2 = null;
boolean anyNotNull = ObjectUtils.anyNotNull(str1, str2); // true
  1. compare(Comparable c1, Comparable c2)

比较两个可比较的对象的大小。可以处理null值。如果c1小于c2,则返回负数;如果c1等于c2,则返回0;如果c1大于c2,则返回正数。

java 复制代码
Integer num1 = 123;
Integer num2 = 456;
int result = ObjectUtils.compare(num1, num2); // -1
  1. min(Comparable... values)

返回一组可比较对象中的最小值,可以处理null值。

java 复制代码
Integer num1 = 123;
Integer num2 = 456;
Integer min = ObjectUtils.min(num1, num2); // 123
  1. max(Comparable... values)

返回一组可比较对象中的最大值,可以处理null值。

java 复制代码
Integer num1 = 123;
Integer num2 = 456;
Integer max = ObjectUtils.max(num1, num2); // 456
  1. clone(Object obj)

克隆一个对象。如果对象实现了Cloneable接口,则调用其clone()方法进行克隆;否则返回null。

java 复制代码
Person person = new Person("John", 30);
Person clone = ObjectUtils.clone(person); // 返回person的克隆对象
相关推荐
pianmian11 小时前
类(JavaBean类)和对象
java
我叫小白菜2 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
Albert Edison2 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
超级小忍3 小时前
JVM 中的垃圾回收算法及垃圾回收器详解
java·jvm
weixin_446122463 小时前
JAVA内存区域划分
java·开发语言·redis
勤奋的小王同学~3 小时前
(javaEE初阶)计算机是如何组成的:CPU基本工作流程 CPU介绍 CPU执行指令的流程 寄存器 程序 进程 进程控制块 线程 线程的执行
java·java-ee
TT哇3 小时前
JavaEE==网站开发
java·redis·java-ee
2401_826097623 小时前
JavaEE-Linux环境部署
java·linux·java-ee
缘来是庄4 小时前
设计模式之访问者模式
java·设计模式·访问者模式
Bug退退退1234 小时前
RabbitMQ 高级特性之死信队列
java·分布式·spring·rabbitmq