Objects类是JAVA.util包下的一个工具类 , 它只拥有私有的构造函数 , 因此无法对其进行实例化 , 但它提供了一系列针对object对象的静态方法 , 包括equal,hash, 参数检查等 。
文章插图
Objects类的所有静态方法
下面对这个工具类的静态方法做具体的介绍:
- equals方法
/* 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方法
/* 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方法
/* 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方法
/* 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方法
/* 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方法
/* 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()));}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 一款可视化 Web 管理后台生成工具
- Python3编写的CMS识别工具 内置安全检测
- JAVA多线程-ConcurrentMap
- 五款精品U盘装机工具评测
- 这些 Java 8 官方挖的坑,你踩过几个?
- 漏洞扫描工具整理
- Fyne - Go语言的跨平台UI工具包
- Alibaba开源Java诊断工具Arthas简单介绍
- Apache Spark优化工具包
- 14个 JavaScript 代码优化技巧