Spring Boot 内置工具类 ObjectUtils

一、对象 ObjectUtils

1.类

import org.springframework.util.ObjectUtils;

2.获取对象的基本信息

// 获取对象的类名。参数为 null 时,返回字符串:"null" 
String nullSafeClassName(Object obj)
// 参数为 null 时,返回 0
int nullSafeHashCode(Object object)
// 参数为 null 时,返回字符串:"null"
String nullSafeToString(boolean[] array)
// 获取对象 HashCode(十六进制形式字符串)。参数为 null 时,返回 0 
String getIdentityHexString(Object obj)
// 获取对象的类名和 HashCode。 参数为 null 时,返回字符串:"" 
String identityToString(Object obj)
// 相当于 toString()方法,但参数为 null 时,返回字符串:""
String getDisplayString(Object obj)

编辑:

Student student = new Student();
student.setAge(18);
student.setName("CSDN");
Student student1 = null;

// String nullSafeClassName(Object obj)  获取对象的类名。参数为 null 时,返回字符串:"null"
ObjectUtils.nullSafeClassName(student);
//输出:com.gemantic.vo.Student
ObjectUtils.nullSafeClassName(student1);
//输出:null

// int nullSafeHashCode(Object object)  参数为 null 时,返回 0
ObjectUtils.nullSafeHashCode(student);
//输出:1950409828
ObjectUtils.nullSafeHashCode(student1);
//输出:0

//String nullSafeToString(boolean[] array)  参数为 null 时,返回字符串:"null"
ObjectUtils.nullSafeToString(student);
// 输出:Student{name='CSDN', age=18}
ObjectUtils.nullSafeToString(student1);
// 输出:null

// String getIdentityHexString(Object obj)  获取对象 HashCode(十六进制形式字符串)。参数为 null 时,返回 0
ObjectUtils.getIdentityHexString(student);
// 输出:7440e464
ObjectUtils.getIdentityHexString(student1);
// 输出:0

//String identityToString(Object obj)   获取对象的类名和 HashCode。 参数为 null 时,返回字符串:""
ObjectUtils.identityToString(student);
// 输出:com.gemantic.vo.Student@7440e464
ObjectUtils.identityToString(student1);
// 输出:字符串:""

//String getDisplayString(Object obj)   相当于 toString()方法,但参数为 null 时,返回字符串:""
ObjectUtils.getDisplayString(student);
// 输出:Student{name='CSDN', age=18}
ObjectUtils.getDisplayString(student1);
// 输出:字符串:""

3.判断工具

// 判断数组是否为空
boolean isEmpty(Object[] array)
// 判断参数对象是否是数组
boolean isArray(Object obj)
// 判断数组中是否包含指定元素
boolean containsElement(Object[] array, Object element)
// 相等,或同为 null时,返回 true
boolean nullSafeEquals(Object o1, Object o2)
/*
判断参数对象是否为空,判断标准为:
    Optional: Optional.empty()
       Array: length == 0
CharSequence: length == 0
  Collection: Collection.isEmpty()
         Map: Map.isEmpty()
 */
boolean isEmpty(Object obj)

测试:

Student student = new Student();
student.setAge(18);
student.setName("CSDN");
Student student1 = null;
Student student2 = new Student();
String[] strArray = {"1","2","3","4","5","6"};
String[] strArray1 = {};

// boolean isEmpty(Object[] array)  判断数组是否为空
ObjectUtils.isEmpty(student);
// 输出:false
ObjectUtils.isEmpty(student1);
// 输出:true

// boolean isArray(Object obj)  判断参数对象是否是数组
ObjectUtils.isArray(student);
// 输出:false
ObjectUtils.isArray(strArray);
// 输出:true

// boolean containsElement(Object[] array, Object element)  判断数组中是否包含指定元素
ObjectUtils.containsElement(strArray,"1");
// 输出:true
ObjectUtils.containsElement(strArray,"8");
// 输出:false

// boolean nullSafeEquals(Object o1, Object o2)  相等,或同为 null时,返回 true
ObjectUtils.nullSafeEquals(student,student);
// 输出:true
ObjectUtils.nullSafeEquals(student,student2);
// 输出:false

// boolean isEmpty(Object obj) 判断参数对象是否为空
ObjectUtils.isEmpty(student);
// 输出:false
ObjectUtils.isEmpty(student1);
// 输出:true
ObjectUtils.isEmpty(strArray);
// 输出:false
ObjectUtils.isEmpty(strArray1);
// 输出:true

4.Array操作

// 向参数数组的末尾追加新元素,并返回一个新数组
<A, O extends A> A[] addObjectToArray(A[] array, O obj)
// 原生基础类型数组 --> 包装类数组
Object[] toObjectArray(Object source)

测试:

String[] strArray = {"1","2","3","4","5","6"};

// <A, O extends A> A[] addObjectToArray(A[] array, O obj)   向参数数组的末尾追加新元素,并返回一个新数组
String[] strArrayNew = ObjectUtils.addObjectToArray(strArray,"8");
System.out.println(Arrays.toString(strArrayNew));
// 输出:[1, 2, 3, 4, 5, 6, 8]

// Object[] toObjectArray(Object source)   原生基础类型数组 --> 包装类数组
Object[] strArray1 = ObjectUtils.toObjectArray(strArrayNew);
System.out.println(Arrays.toString(strArray1));
// 输出:[1, 2, 3, 4, 5, 6, 8]
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值