• 分析Java 8中 Objects 类源码


    Objects 类位于 java.util 包下,自 JDK 1.7 版本新增的,它是一个 final 类,不能被继承,且构造函数是 private 的,不能被实例化,它提供了一系列操作Object对象的静态方法,通常会被当做工具类去使用,其类定义如下:

    class="hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public final class Objects {}
    

    Objects 类在 jdk 源码中应用很广泛,通过 Intellij Idea 搜索一下在 rt.jar 包中的应用,部分截图如下:

    ::: hljs-center

    :::

    从上面的搜索结果可以看出,在 jdk 1.8 版本中,大概有 366 处使用了 Objects 类的相关方法,下面结合具体的源码来分析一下。

    1、构造函数

    Objects 类不能被实例化,其构造函数是私有的,实现如下:

    1. class="prettyprint hljs cpp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">private Objects() {
    2. throw new AssertionError("No java.util.Objects instances for you!");
    3. }

    2、方法

    Objects 类提供的静态方法大致可以分为以下的几类:

    • equals :比较两个对象是否相同
    • hash :获取对象的哈希值;
    • toString :将对象转换为字符串
    • requireNonNull :要求对象不为空
    • isNull :判断对象是否为空
    • nonNull :判断对象不为空

    具体可以通过下面的结构图来看看这些方法:

    ::: hljs-center

    :::

    2.1 equals 方法

    equals() 方法主要用于比较两个指定的对象是否相同,定义如下:

    1. class="prettyprint hljs delphi" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static boolean equals(Object a, Object b) {
    2. return (a == b) || (a != null && a.equals(b));
    3. }

    除了 equals() 方法外,还有一个 deepEquals() 方法,该方法用于比较两个对象是否完全相同,可用于比较两个数组对象是否相同,其定义如下:

    1. class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static boolean deepEquals(Object a, Object b) {
    2. if (a == b)
    3. return true;
    4. else if (a == null || b == null)
    5. return false;
    6. else
    7. // 对数组元素进行比较
    8. return Arrays.deepEquals0(a, b);
    9. }

    在比较数组对象是否相同时,具体调用的是 Arrays.deepEquals0() 方法,其实现如下:

    1. "prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">static boolean deepEquals0(Object e1, Object e2) {
    2. assert e1 != null;
    3. boolean eq;
    4. if (e1 instanceof Object[] && e2 instanceof Object[])
    5. eq = deepEquals ((Object[]) e1, (Object[]) e2);
    6. else if (e1 instanceof byte[] && e2 instanceof byte[])
    7. eq = equals((byte[]) e1, (byte[]) e2);
    8. else if (e1 instanceof short[] && e2 instanceof short[])
    9. eq = equals((short[]) e1, (short[]) e2);
    10. else if (e1 instanceof int[] && e2 instanceof int[])
    11. eq = equals((int[]) e1, (int[]) e2);
    12. else if (e1 instanceof long[] && e2 instanceof long[])
    13. eq = equals((long[]) e1, (long[]) e2);
    14. else if (e1 instanceof char[] && e2 instanceof char[])
    15. eq = equals((char[]) e1, (char[]) e2);
    16. else if (e1 instanceof float[] && e2 instanceof float[])
    17. eq = equals((float[]) e1, (float[]) e2);
    18. else if (e1 instanceof double[] && e2 instanceof double[])
    19. eq = equals((double[]) e1, (double[]) e2);
    20. else if (e1 instanceof boolean[] && e2 instanceof boolean[])
    21. eq = equals((boolean[]) e1, (boolean[]) e2);
    22. else
    23. // 如果不是数组类型,则直接使用 Object.equals() 方法比较
    24. eq = e1.equals(e2);
    25. return eq;
    26. }

    上面的方法实现中,会逐个比较数组中的元素是否相同,如果都相同,则两个数组相同。

    2.2 hashCode 方法

    hashCode() 方法用于获取对象的哈希值,其实现如下:

    1. class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static int hashCode(Object o) {
    2. // 如果对象不为空,则调用 Object 的 hashCode() 方法
    3. // 否则直接返回 0
    4. return o != null ? o.hashCode() : 0;
    5. }

    除了可以获取单个对象的哈希值外, Objects 还提供了可以获取多个对象的哈希值,即 hash() 方法,如下:

    1. class="prettyprint hljs cpp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static int hash(Object... values) {
    2. return Arrays.hashCode(values);
    3. }

    该方法的参数为可变长参数,具体实现是调用 Arrays.hashCode 方法,其源码如下:

    1. class="prettyprint hljs groovy" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static int hashCode(Object a[]) {
    2. if (a == null)
    3. return 0;
    4. int result = 1;
    5. for (Object element : a)
    6. result = 31 * result + (element == null ? 0 : element.hashCode());
    7. return result;
    8. }

    从上面的实现可以看出,数组元素的哈希值是所有元素的哈希值经过一定变换后的和。

    2.3 toString 方法

    toString() 方法就是将对象转换为字符串,如果对象为 null ,则会直接返回 null 字符串,其实现如下:

    1. class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static String toString(Object o) {
    2. return String.valueOf(o);
    3. }
    4. public static String toString(Object o, String nullDefault) {
    5. // 如果对象为null,则可以返回一个默认值
    6. return (o != null) ? o.toString() : nullDefault;
    7. }

    toString() 方法具体实现调用的是 String.valueOf() 方法,而 String.valueOf() 方法又会调用 Object.toString() 方法,其源码如下:

    1. class="prettyprint hljs typescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static String valueOf(Object obj) {
    2. return (obj == null) ? "null" : obj.toString();
    3. }

    2.4 requireNonNull 方法

    requireNonNull() 方法用于判定参数对象是否为空,如果对象为空,则会抛出空指针异常,其实现如下:

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static  T requireNonNull(T obj) {
    2. // 直接抛出空指针异常
    3. if (obj == null)
    4. throw new NullPointerException();
    5. return obj;
    6. }
    7. public static T requireNonNull(T obj, String message) {
    8. // 抛出指定信息的空指针异常
    9. if (obj == null)
    10. throw new NullPointerException(message);
    11. return obj;
    12. }
    13. public static T requireNonNull(T obj, Supplier<String> messageSupplier) {
    14. // 通过一个 Supplier 来获取空指针异常信息
    15. if (obj == null)
    16. throw new NullPointerException(messageSupplier.get());
    17. return obj;
    18. }

    requireNonNull() 有3个重载方法,可以指定抛出空指针异常时的信息,对于第三个方法,可以传入一个 Supplier ,在对象为空时,动态的构建空指针异常信息,如果一个方法要求参数不能为空,那么就可以直接使用该方法对参数进行空校验。

    2.5 isNull 方法

    isNull() 方法判定对象为空,其实现比较简单,如下:

    1. class="hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static boolean isNull(Object obj) {
    2. return obj == null;
    3. }

    在平时的编程过程中,如果需要判断对象等于 null 时,也可以使用 Objects.isNull() 方法替代。

    2.6 nonNull 方法

    和 isNull() 方法相反, nonNull() 方法用于判断对象不为空,其实现也比较简单,如下:

    1. class="hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static boolean nonNull(Object obj) {
    2. return obj != null;
    3. }

    整个 Objects 类的源码还是比较少的,主要作为一个工具类去使用,对于平时的空判断、非空判断或者参数校验,都可以尝试使用 Objects 类替代,可以使代码更加简洁。

  • 相关阅读:
    jmeter请求接口问题小记
    【云原生之Docker实战】使用Docker部署openwrt软路由系统
    超详细!linux系统nlg-eval安装指南
    vue项目 Editor.md使用示例
    Tauri-Vue3桌面端聊天室|tauri+vite3仿微信|tauri聊天程序EXE
    大模型Agent最新论文及源码合集,覆盖构建、应用、评估
    一年前端|17K|极光推送5轮面经
    聊聊Redis sentinel 机制
    小程序开发 二 (王红元)
    倍福PLC和C#通过ADS通信传输String数组类型变量
  • 原文地址:https://blog.csdn.net/Java_ttcd/article/details/126243738