org.springframework.util.ObjectUtils
org.springframework.util.ObjectUtils
获取对象的基本信息:
String str = null; // 获取对象的类名。参数为 null 时,返回字符串:"null" String s = ObjectUtils.nullSafeClassName(str); System.out.println(s);// null // 参数为 null 时,返回 0 int i = ObjectUtils.nullSafeHashCode(str); System.out.println(i);// 0 // 参数为 null 时,返回字符串:"null" String s1 = ObjectUtils.nullSafeToString(str); System.out.println(s1);// null // 获取对象 HashCode(十六进制形式字符串)。参数为 null 时,返回 0 String identityHexString = ObjectUtils.getIdentityHexString(str); System.out.println(identityHexString);// 0 // 获取对象的类名和 HashCode。 参数为 null 时,返回字符串:"" String s2 = ObjectUtils.identityToString(str); System.out.println(s2); // 相当于 toString()方法,但参数为 null 时,返回字符串:"" String displayString = ObjectUtils.getDisplayString(str); System.out.println(displayString);
判断方法:
// 判断数组是否为空 boolean empty = ObjectUtils.isEmpty(strArr); System.out.println(empty);// true // 判断参数对象是否是数组 boolean array = ObjectUtils.isArray(strArr); System.out.println(array);// true // 判断数组中是否包含指定元素 boolean aa1 = ObjectUtils.containsElement(strArr2, "aa1"); System.out.println(aa1);// true // 相等,或同为 null时,返回 true boolean b = ObjectUtils.nullSafeEquals(null, null); System.out.println(b);// true
其他方法:
// 向参数数组的末尾追加新元素,并返回一个新数组 Integer[] integers = ObjectUtils.addObjectToArray(intArr2, 4); System.out.println(ArrayUtils.toString(integers));//{1,2,3,4} // 原生基础类型数组 --> 包装类数组 Object[] objects = ObjectUtils.toObjectArray(intArr); System.out.println(ArrayUtils.toString(objects));//{1,2,3}