Java - Objects工具类

Objects类是JAVA.util包下的一个工具类 , 它只拥有私有的构造函数 , 因此无法对其进行实例化 , 但它提供了一系列针对object对象的静态方法 , 包括equal,hash, 参数检查等 。

Java - Objects工具类

文章插图
Objects类的所有静态方法
下面对这个工具类的静态方法做具体的介绍:
  • equals方法
判断2个object对象是否相等 。
/* Since 1.7 */ public static boolean equals(Object a, Object b) {return (a == b) || (a != null && a.equals(b));}例子:
private static void testEquals() {System.out.println("null equals null:" + Objects.equals(null,null));System.out.println("1 equals null:" + Objects.equals(1,null));System.out.println("null equals 1:" + Objects.equals(null,1));System.out.println("ABC equals ABC:" + Objects.equals("ABC","ABC"));System.out.println("1 equals ABC:" + Objects.equals(1,"ABC"));}输出结果:
null equals null:true1 equals null:falsenull equals 1:falseABC equals ABC:true1 equals ABC:false
  • deepEquals方法
判断2个对象是否相等 , 如果是对象是数组 , 将会对数组的元素进行比较 。
/* Since 1.7 */public static boolean deepEquals(Object a, Object b) {if (a == b)return true;else if (a == null || b == null)return false;elsereturn Arrays.deepEquals0(a, b);}例子:
private static void testDeepEquals() {System.out.println("null equals null:" + Objects.deepEquals(null,null));System.out.println("1 equals null:" + Objects.deepEquals(1,null));System.out.println("null equals 1:" + Objects.deepEquals(null,1));System.out.println("ABC equals ABC:" + Objects.deepEquals("ABC","ABC"));System.out.println("1 equals ABC:" + Objects.deepEquals(1,"ABC"));System.out.println("string array equals string array:" + Objects.deepEquals(new String[]{"1","2"},new String[]{"1","2"}));System.out.println("int array equals string array:" + Objects.deepEquals(new Integer[]{1,2},new String[]{"1","2"}));}输出结果:
null equals null:true1 equals null:falsenull equals 1:falseABC equals ABC:true1 equals ABC:falsestring array equals string array:trueint array equals string array:false
  • hashCode方法
获取对象的hash码 , 如果object为null , 返回0 。
/* Since 1.7 */ public static int hashCode(Object o) {return o != null ? o.hashCode() : 0;}例子:
private static void testHashCode() {System.out.println("hash for int:" + Objects.hashCode(1));System.out.println("hash for string:" + Objects.hashCode("1"));System.out.println("hash for long:" + Objects.hashCode(1L));System.out.println("hash for double:" + Objects.hashCode(1D));System.out.println("hash for null:" + Objects.hashCode(null));}输出:
hash for int:1hash for string:49hash for long:1hash for double:1072693248hash for null:0
  • hash方法
计算数组的hash值(since 1.7) 。
/* Since 1.7 */public static int hash(Object... values) {return Arrays.hashCode(values);}例子:
private static void testHash() {System.out.println("int array hash:" + Objects.hash(1,2,4));System.out.println("string array hash:" + Objects.hash("1","2","4"));System.out.println("mixed array hash:" + Objects.hash("1",2,"4"));}输出:
int array hash:30818string array hash:78482mixed array hash:76994
  • toString方法
把object对象转换成字符串 。
/* Since 1.7 */public static String toString(Object o) {return String.valueOf(o);}/* Since 1.7 */public static String toString(Object o, String nullDefault) {return (o != null) ? o.toString() : nullDefault;}例子:
private static void testToString() {System.out.println("int str:" + Objects.toString(1));System.out.println("double str:" + Objects.toString(1.01D));System.out.println("null:" + Objects.toString(null, "1000"));}输出结果:
int str:1double str:1.01null:1000
  • compare方法
使用指定的Comparator来对2个值进行比较 。
/* Since 1.7 */public static <T> int compare(T a, T b, Comparator<? super T> c) {return (a == b) ? 0 :c.compare(a, b);}例子:
private static void testCompare() {System.out.println("int compare 1:" + Objects.compare(1,2, Comparator.naturalOrder()));System.out.println("int compare 2:" + Objects.compare(1,2, Comparator.reverseorder()));}


推荐阅读