前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【commons-lang3专题】005- ObjectUtils 专题

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

作者头像
訾博ZiBo
发布2025-01-06 14:46:37
发布2025-01-06 14:46:37
510
举报
文章被收录于专栏:全栈开发工程师

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

〇、准备

1、ObejctUtils 主要作用

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

2、引入依赖

代码语言:javascript
复制
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

3、实体类

Dog
代码语言:javascript
复制
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;
}
DogCloneable
代码语言:javascript
复制
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、判断给定数组中的任何元素值是否都不是 null

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 2、判断给定数组中的任何元素值是否都是 null
System.out.println(ObjectUtils.allNull(null, null, null)); // true
System.out.println(ObjectUtils.allNull("a", null, null)); // false

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

代码语言:javascript
复制
// 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 的值

代码语言:javascript
复制
// 4、判断给定数组中的元素是否有是 null 的值
System.out.println(ObjectUtils.anyNull("a", "b", "c")); // false
System.out.println(ObjectUtils.anyNull(null, "b", "c")); // true

二、克隆对象-浅克隆

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

代码语言:javascript
复制
// 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 ,则返回原对象-浅克隆

代码语言:javascript
复制
// 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、比较大小

代码语言:javascript
复制
// 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值更大

代码语言:javascript
复制
// 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 ,返回默认值

代码语言:javascript
复制
// 9、如果对象为 null ,返回默认值
System.out.println(ObjectUtils.defaultIfNull(null, "默认值")); // 默认值

五、对象判空

10、判断对象是否为空

代码语言:javascript
复制
// 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、判断对象是否非空

代码语言:javascript
复制
// 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、获取最大值

代码语言:javascript
复制
// 12、获取最大值
System.out.println(ObjectUtils.max(1, 2, 3, 4, 5)); // 5

13、获取最小值

代码语言:javascript
复制
// 13、获取最小值
System.out.println(ObjectUtils.min(1, 2, 3, 4, 5)); // 1

14、获取中位数

代码语言:javascript
复制
// 14、获取中位数
System.out.println(ObjectUtils.median(1, 2, 3, 4, 5)); // 3

七、完整代码

代码语言:javascript
复制
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
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 【commons-lang3专题】005- ObjectUtils 专题
  • 〇、准备
    • 1、ObejctUtils 主要作用
    • 2、引入依赖
    • 3、实体类
      • Dog
      • DogCloneable
  • 一、判断空与非空
    • 1、判断给定数组中的任何元素值是否都不是 null
    • 2、判断给定数组中的任何元素值是否都是 null
    • 3、判断给定数组中的元素是否有不是 null 的值
    • 4、判断给定数组中的元素是否有是 null 的值
  • 二、克隆对象-浅克隆
    • 5、克隆对象-如果对象实现Cloneable则为克隆,否则为null-浅克隆
    • 6、克隆对象,如果返回 null ,则返回原对象-浅克隆
  • 三、比较大小
    • 7、比较大小
    • 8、比较大小-null值更大
  • 四、为 null 默认值
    • 9、如果对象为 null ,返回默认值
  • 五、对象判空
    • 10、判断对象是否为空
    • 11、判断对象是否非空
  • 六、获取极值
    • 12、获取最大值
    • 13、获取最小值
    • 14、获取中位数
  • 七、完整代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档