import org.springframework.util.ObjectUtils;
// 获取对象的类名。参数为 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);
// 输出:字符串:""
// 判断数组是否为空
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
// 向参数数组的末尾追加新元素,并返回一个新数组
<A, O extends A> A[] addObjectToArray(A[] array, O obj)
// 原生基础类型数组 --> 包装类数组
Object[] toObjectArray(Object source)
测试:
String[] strArray = {"1","2","3","4","5","6"};
// 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]