• 【commons-lang3专题】005- ObjectUtils 专题


    【commons-lang3专题】005- ObjectUtils 专题

    〇、准备

    1、ObejctUtils 主要作用

    提供生成各种对象操作方法。

    2、引入依赖

    
    <dependency>
        <groupId>org.apache.commonsgroupId>
        <artifactId>commons-lang3artifactId>
        <version>3.12.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3、实体类

    Dog

    package com.zibo.zibo2022.object_utils.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Dog {
        /**
         * 名字
         */
        private String name;
    
        /**
         * 年龄
         */
        private Integer age;
    
        /**
         * son
         */
        private Dog son;
    }
    
    • 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

    DogCloneable

    package com.zibo.zibo2022.object_utils.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class DogCloneable implements Cloneable {
    
        /**
         * 名字
         */
        private String name;
    
        /**
         * 年龄
         */
        private Integer age;
    
        /**
         * son
         */
        private DogCloneable son;
    
        // 下面的方法是使用 idea 生成的
        @Override
        public DogCloneable clone() {
            try {
                DogCloneable clone = (DogCloneable) super.clone();
                // TODO: copy mutable state here, so the clone can't change the internals of the original
                return clone;
            } catch (CloneNotSupportedException e) {
                throw new AssertionError();
            }
        }
    }
    
    • 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

    一、判断空与非空

    1、判断给定数组中的任何元素值是否都不是 null

    // 1、判断给定数组中的任何元素值是否都不是 null
    System.out.println(ObjectUtils.allNotNull("a", "b", "c")); // true
    System.out.println(ObjectUtils.allNotNull("", "b", "c")); // true
    System.out.println(ObjectUtils.allNotNull(null, "b", "c")); // false
    
    • 1
    • 2
    • 3
    • 4

    2、判断给定数组中的任何元素值是否都是 null

    // 2、判断给定数组中的任何元素值是否都是 null
    System.out.println(ObjectUtils.allNull(null, null, null)); // true
    System.out.println(ObjectUtils.allNull("a", null, null)); // false
    
    • 1
    • 2
    • 3

    3、判断给定数组中的元素是否有不是 null 的值

    // 3、判断给定数组中的元素是否有不是 null 的值
    System.out.println(ObjectUtils.anyNotNull("a", "b", "c")); // true
    System.out.println(ObjectUtils.anyNotNull(null, "b", "c")); // true
    System.out.println(ObjectUtils.anyNotNull(null, null, null)); // false
    
    • 1
    • 2
    • 3
    • 4

    4、判断给定数组中的元素是否有是 null 的值

    // 4、判断给定数组中的元素是否有是 null 的值
    System.out.println(ObjectUtils.anyNull("a", "b", "c")); // false
    System.out.println(ObjectUtils.anyNull(null, "b", "c")); // true
    
    • 1
    • 2
    • 3

    二、克隆对象-浅克隆

    5、克隆对象-如果对象实现Cloneable则为克隆,否则为null-浅克隆

    // 5、克隆对象-如果对象实现Cloneable则为克隆,否则为null-浅克隆
    DogCloneable son = new DogCloneable("son", 1, null);
    DogCloneable dog = new DogCloneable("zibo", 2, son);
    System.out.println(dog);
    // DogCloneable(name=zibo, age=2, son=DogCloneable(name=son, age=1, son=null))
    DogCloneable cloneDog = ObjectUtils.clone(dog);
    System.out.println(cloneDog);
    // DogCloneable(name=zibo, age=2, son=DogCloneable(name=son, age=1, son=null))
    son.setName("son2");
    System.out.println(cloneDog);
    // 浅克隆
    // DogCloneable(name=zibo, age=2, son=DogCloneable(name=son2, age=1, son=null))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    6、克隆对象,如果返回 null ,则返回原对象-浅克隆

    // 6、克隆对象,如果返回 `null` ,则返回原对象-浅克隆
    // 先调 clone(final T obj) ,如果返回 null,则返回原来的 obj 对象
    Dog son1 = new Dog("son", 1, null);
    Dog dog1 = new Dog("zibo", 2, son1);
    Dog cloneIfPossible = ObjectUtils.cloneIfPossible(dog1);
    System.out.println(cloneIfPossible);
    // 未实现 `Cloneable` 接口,返回 `null` ,返回原对象
    // Dog(name=zibo, age=2, son=Dog(name=son, age=1, son=null))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    三、比较大小

    7、比较大小

    // 7、比较大小
    // 语法:int compare(T c1, T c2)
    // 规则:如果 c1 < c2,则返回 -1 ;如果 c1 > c2,则返回 +1 ;如果 c1 = c2,则返回 0;
    System.out.println(ObjectUtils.compare(1, 20)); // -1
    System.out.println(ObjectUtils.compare(20, 1)); // 1
    System.out.println(ObjectUtils.compare(1, 1)); // 0
    System.out.println(ObjectUtils.compare("1", "1")); // 0
    System.out.println(ObjectUtils.compare("10", "1")); // 1
    System.out.println(ObjectUtils.compare("a", "a")); // 0
    System.out.println(ObjectUtils.compare("a", "b")); // -1
    System.out.println(ObjectUtils.compare(null, "b")); // -1
    System.out.println(ObjectUtils.compare("a", null)); // 1
    System.out.println();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    8、比较大小-null值更大

    // 8、比较大小-null值更大
    // 注意与上面的不同
    System.out.println(ObjectUtils.compare(1, 20, true)); // -1
    System.out.println(ObjectUtils.compare(null, 20, true)); // 1
    System.out.println(ObjectUtils.compare(1, null, true)); // -1
    System.out.println(ObjectUtils.compare(null, null, true)); // 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    四、为 null 默认值

    9、如果对象为 null ,返回默认值

    // 9、如果对象为 null ,返回默认值
    System.out.println(ObjectUtils.defaultIfNull(null, "默认值")); // 默认值
    
    • 1
    • 2

    五、对象判空

    10、判断对象是否为空

    // 10、判断对象是否为空
    // 支持:CharSequence、Array、Collection、Map
    System.out.println(ObjectUtils.isEmpty("")); // true
    System.out.println(ObjectUtils.isEmpty(Arrays.asList("hello", "world"))); // false
    System.out.println(ObjectUtils.isEmpty(new HashMap<>())); // true
    System.out.println(ObjectUtils.isEmpty(new Integer[]{})); // true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    11、判断对象是否非空

    // 11、判断对象是否非空
    // 支持:CharSequence、Array、Collection、Map
    System.out.println(ObjectUtils.isNotEmpty("")); // false
    System.out.println(ObjectUtils.isNotEmpty(Arrays.asList("hello", "world"))); // true
    System.out.println(ObjectUtils.isNotEmpty(new HashMap<>())); // false
    System.out.println(ObjectUtils.isNotEmpty(new Integer[]{})); // false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    六、获取极值

    12、获取最大值

    // 12、获取最大值
    System.out.println(ObjectUtils.max(1, 2, 3, 4, 5)); // 5
    
    • 1
    • 2

    13、获取最小值

    // 13、获取最小值
    System.out.println(ObjectUtils.min(1, 2, 3, 4, 5)); // 1
    
    • 1
    • 2

    14、获取中位数

    // 14、获取中位数
    System.out.println(ObjectUtils.median(1, 2, 3, 4, 5)); // 3
    
    • 1
    • 2

    七、完整代码

    package com.zibo.zibo2022.object_utils.main;
    
    import com.zibo.zibo2022.object_utils.entity.Dog;
    import com.zibo.zibo2022.object_utils.entity.DogCloneable;
    import org.apache.commons.lang3.ObjectUtils;
    
    import java.util.Arrays;
    import java.util.HashMap;
    
    public class Main {
    
        public static void main(String[] args) {
            // start
            // 一、判断空与非空
            // 1、判断给定数组中的任何元素值是否都不是 null
            System.out.println(ObjectUtils.allNotNull("a", "b", "c")); // true
            System.out.println(ObjectUtils.allNotNull("", "b", "c")); // true
            System.out.println(ObjectUtils.allNotNull(null, "b", "c")); // false
    
            // 2、判断给定数组中的任何元素值是否都是 null
            System.out.println(ObjectUtils.allNull(null, null, null)); // true
            System.out.println(ObjectUtils.allNull("a", null, null)); // false
    
            // 3、判断给定数组中的元素是否有不是 null 的值
            System.out.println(ObjectUtils.anyNotNull("a", "b", "c")); // true
            System.out.println(ObjectUtils.anyNotNull(null, "b", "c")); // true
            System.out.println(ObjectUtils.anyNotNull(null, null, null)); // false
    
            // 4、判断给定数组中的元素是否有是 null 的值
            System.out.println(ObjectUtils.anyNull("a", "b", "c")); // false
            System.out.println(ObjectUtils.anyNull(null, "b", "c")); // true
    
            // 二、克隆对象-浅克隆
            // 5、克隆对象-如果对象实现Cloneable则为克隆,否则为null-浅克隆
            DogCloneable son = new DogCloneable("son", 1, null);
            DogCloneable dog = new DogCloneable("zibo", 2, son);
            System.out.println(dog);
            // DogCloneable(name=zibo, age=2, son=DogCloneable(name=son, age=1, son=null))
            DogCloneable cloneDog = ObjectUtils.clone(dog);
            System.out.println(cloneDog);
            // DogCloneable(name=zibo, age=2, son=DogCloneable(name=son, age=1, son=null))
            son.setName("son2");
            System.out.println(cloneDog);
            // 浅克隆
            // DogCloneable(name=zibo, age=2, son=DogCloneable(name=son2, age=1, son=null))
    
            // 6、克隆对象,如果返回 `null` ,则返回原对象-浅克隆
            // 先调 clone(final T obj) ,如果返回 null,则返回原来的 obj 对象
            Dog son1 = new Dog("son", 1, null);
            Dog dog1 = new Dog("zibo", 2, son1);
            Dog cloneIfPossible = ObjectUtils.cloneIfPossible(dog1);
            System.out.println(cloneIfPossible);
            // 未实现 `Cloneable` 接口,返回 `null` ,返回原对象
            // Dog(name=zibo, age=2, son=Dog(name=son, age=1, son=null))
    
            // 三、比较大小
            // 7、比较大小
            // 语法:int compare(T c1, T c2)
            // 规则:如果 c1 < c2,则返回 -1 ;如果 c1 > c2,则返回 +1 ;如果 c1 = c2,则返回 0;
            System.out.println(ObjectUtils.compare(1, 20)); // -1
            System.out.println(ObjectUtils.compare(20, 1)); // 1
            System.out.println(ObjectUtils.compare(1, 1)); // 0
            System.out.println(ObjectUtils.compare("1", "1")); // 0
            System.out.println(ObjectUtils.compare("10", "1")); // 1
            System.out.println(ObjectUtils.compare("a", "a")); // 0
            System.out.println(ObjectUtils.compare("a", "b")); // -1
            System.out.println(ObjectUtils.compare(null, "b")); // -1
            System.out.println(ObjectUtils.compare("a", null)); // 1
            System.out.println();
    
            // 8、比较大小-null值更大
            // 注意与上面的不同
            System.out.println(ObjectUtils.compare(1, 20, true)); // -1
            System.out.println(ObjectUtils.compare(null, 20, true)); // 1
            System.out.println(ObjectUtils.compare(1, null, true)); // -1
            System.out.println(ObjectUtils.compare(null, null, true)); // 0
    
            // 四、为 `null` 默认值
            // 9、如果对象为 null ,返回默认值
            System.out.println(ObjectUtils.defaultIfNull(null, "默认值")); // 默认值
    
            // 五、对象判空
            // 10、判断对象是否为空
            // 支持:CharSequence、Array、Collection、Map
            System.out.println(ObjectUtils.isEmpty("")); // true
            System.out.println(ObjectUtils.isEmpty(Arrays.asList("hello", "world"))); // false
            System.out.println(ObjectUtils.isEmpty(new HashMap<>())); // true
            System.out.println(ObjectUtils.isEmpty(new Integer[]{})); // true
    
            // 11、判断对象是否非空
            // 支持:CharSequence、Array、Collection、Map
            System.out.println(ObjectUtils.isNotEmpty("")); // false
            System.out.println(ObjectUtils.isNotEmpty(Arrays.asList("hello", "world"))); // true
            System.out.println(ObjectUtils.isNotEmpty(new HashMap<>())); // false
            System.out.println(ObjectUtils.isNotEmpty(new Integer[]{})); // false
    
            // 六、获取极值
            // 12、获取最大值
            System.out.println(ObjectUtils.max(1, 2, 3, 4, 5)); // 5
    
            // 13、获取最小值
            System.out.println(ObjectUtils.min(1, 2, 3, 4, 5)); // 1
    
            // 14、获取中位数
            System.out.println(ObjectUtils.median(1, 2, 3, 4, 5)); // 3
            // end
        }
    
    }
    
    • 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
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
  • 相关阅读:
    每日一题 213. 打家劫舍 II
    离线强化学习(Offline RL)系列7: (状态处理)Koopman-Q学习:通过动力学对称性的状态空间数据增强方法
    (202402)多智能体MetaGPT入门2:AI Agent知识体系结构
    ElasticSearch之结构化搜索
    scrollIntoView()方法的学习
    复现yolov5+Deepsort实现车辆行人的检测、追踪和计数
    python3使用libpcap库进行抓包及数据处理
    《代码大全2》第5章 软件构建中的设计
    数仓(三)
    C/C++输出绝对值 2019年9月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
  • 原文地址:https://blog.csdn.net/qq_29689343/article/details/126025671