当前位置:网站首页>【commons-lang3专题】005- ObjectUtils 专题
【commons-lang3专题】005- ObjectUtils 专题
2022-07-28 23:29:00 【訾博ZiBo】
【commons-lang3专题】005- ObjectUtils 专题
文章目录
〇、准备
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
}
}
边栏推荐
- Five interesting magic commands in jupyter notebook
- PTA (daily question) 7-77 encryption
- rk3399 9.0驱动添加Powser按键
- Alibaba code index technology practice: provide reading experience of local IDE for code review
- [develop low code platform] low code rendering
- Some operations of Ubuntu remote server configuration database (unable to locate package MySQL server, steps of installing mysql, unable to enter password when logging in MySQL)
- Oracle实例无法启动的问题如何解决
- Copy the table in word to wechat as a picture and send it
- Shell编程规范与变量
- NFTScan 与 NFTPlay 在 NFT 数据领域达成战略合作
猜你喜欢
随机推荐
DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use
The download file of native JS implementation can be used anywhere
SDRAM控制器设计(数字控制器的两种设计方法)
What are the skills of API interface optimization?
Longest ascending subsequence
(20211130更新)关于jupyter notebook的下载安装及自己的配置、主题
DRF - deserialization of serializer, fields and parameters, local and global hooks, use of modelserializer
Summary of preprocessing methods for time series data
Some considerations about ThreadPool
【MySQL 8】Generated Invisible Primary Keys(GIPK)
Kwai focuses on regulating the number maintenance behavior in the ways of handling and manuscript washing, and how to purify the content ecology on the we media platform
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
Still writing a lot of if to judge? A rule executor kills all if judgments in the project
保护性拷贝&无状态
The 30th day of question brushing
主线程与守护线程
DRF -- authentication, authority, frequency source code analysis, global exception handling, automatic generation of interface documents, RBAC introduction
selenium对接代理与seleniumwire访问开发者工具NetWork
[develop low code platform] low code rendering
Requestvideoframecallback() simple instance