• Spring Boot 内置工具类 ObjectUtils


    一、对象 ObjectUtils

    1.类

    import org.springframework.util.ObjectUtils;
    
    • 1

    2.获取对象的基本信息

    // 获取对象的类名。参数为 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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    编辑:

    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);
    // 输出:字符串:""
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    3.判断工具

    // 判断数组是否为空
    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    测试:

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    4.Array操作

    // 向参数数组的末尾追加新元素,并返回一个新数组
    <A, O extends A> A[] addObjectToArray(A[] array, O obj)
    // 原生基础类型数组 --> 包装类数组
    Object[] toObjectArray(Object source)
    
    • 1
    • 2
    • 3
    • 4

    测试:

    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]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    QML实现文件十六进制数据展示
    什么是强制缓存?什么是协商缓存?cache
    【计算机网络】UDP/TCP 协议
    Grafana变量默认全选
    [笔记]Springboot入门《五》之单元测试读取配置
    含文档+PPT+源码等]精品基于Uniapp+SSM实现的校园心理健康APP[包运行成功]计算机毕业设计安卓项目源码
    概率论中的几个重要悖论问题
    【TensorFlow1.X】系列学习笔记【入门二】
    年后求职找B端产品经理?差点把自己坑惨了......
    共享门店系统开发如何实际运用?
  • 原文地址:https://blog.csdn.net/qq_41525524/article/details/127650991