• 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
  • 相关阅读:
    计算机网络性能指标:速率,带宽,吞吐量
    全网最牛自动化测试框架系列之pytest(13)-多线程、多进程执行用例
    竞赛 基于深度学习的行人重识别(person reid)
    SSM整合Redis&注解式缓存的使用
    JavaWeb在线商城系统(java+jsp+servlet+MySQL+jdbc+css+js+jQuery)
    11:STM32---spl通信
    Vue Admin Template关闭eslint校验,lintOnSave:false设置无效解决办法
    简说webpack plugin
    Order by注入
    2021 CCPC(Harbin)-B. Magical Subsequence
  • 原文地址:https://blog.csdn.net/qq_41525524/article/details/127650991