0
点赞
收藏
分享

微信扫一扫

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


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

文章目录

  • ​​【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、获取中位数​​
  • ​​七、完整代码​​

〇、准备

1、ObejctUtils 主要作用

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

2、引入依赖

<!-- 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

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

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

// 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

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

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

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

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

二、克隆对象-浅克隆

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))

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))

三、比较大小

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();

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

四、为 null 默认值

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

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

五、对象判空

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

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

六、获取极值

12、获取最大值

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

13、获取最小值

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

14、获取中位数

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

七、完整代码

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
}

}


举报

相关推荐

0 条评论