当前位置:网站首页>[Commons lang3 topic] 005- objectutils topic
[Commons lang3 topic] 005- objectutils topic
2022-07-29 00:58:00 【Zibo Zibo】
【commons-lang3 project 】005- ObjectUtils project
List of articles
〇、 Get ready
1、ObejctUtils The main role
Provide and generate various The object operation Method .
2、 Introduce dependencies
<!-- 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、 Entity class
Dog
package com.zibo.zibo2022.object_utils.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dog {
/** * name */
private String name;
/** * Age */
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 {
/** * name */
private String name;
/** * Age */
private Integer age;
/** * son */
private DogCloneable son;
// The following method is to use idea Generated
@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();
}
}
}
One 、 Judge empty and non empty
1、 Determine whether the value of any element in a given array is not null
// 1、 Determine whether the value of any element in a given array is not 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、 Determine whether the value of any element in a given array is null
// 2、 Determine whether the value of any element in a given array is null
System.out.println(ObjectUtils.allNull(null, null, null)); // true
System.out.println(ObjectUtils.allNull("a", null, null)); // false
3、 Determine whether the elements in a given array have no null Value
// 3、 Determine whether the elements in a given array have no null Value
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、 Determine whether the elements in a given array have yes null Value
// 4、 Determine whether the elements in a given array have yes null Value
System.out.println(ObjectUtils.anyNull("a", "b", "c")); // false
System.out.println(ObjectUtils.anyNull(null, "b", "c")); // true
Two 、 Clone objects - Shallow clone
5、 Clone objects - If the object implements Cloneable Clone , Otherwise null- Shallow clone
// 5、 Clone objects - If the object implements Cloneable Clone , Otherwise null- Shallow clone
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);
// Shallow clone
// DogCloneable(name=zibo, age=2, son=DogCloneable(name=son2, age=1, son=null))
6、 Clone objects , If you return null , Returns the original object - Shallow clone
// 6、 Clone objects , If you return `null` , Returns the original object - Shallow clone
// First tune clone(final T obj) , If you return null, Return to the original obj object
Dog son1 = new Dog("son", 1, null);
Dog dog1 = new Dog("zibo", 2, son1);
Dog cloneIfPossible = ObjectUtils.cloneIfPossible(dog1);
System.out.println(cloneIfPossible);
// Unrealized `Cloneable` Interface , return `null` , Return to the original object
// Dog(name=zibo, age=2, son=Dog(name=son, age=1, son=null))
3、 ... and 、 Compare the size
7、 Compare the size
// 7、 Compare the size
// grammar :int compare(T c1, T c2)
// The rules : If c1 < c2, Then return to -1 ; If c1 > c2, Then return to +1 ; If c1 = c2, Then return to 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、 Compare the size -null More valuable
// 8、 Compare the size -null More valuable
// Note the difference from the above
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
Four 、 by null The default value is
9、 If the object is null , Return default
// 9、 If the object is null , Return default
System.out.println(ObjectUtils.defaultIfNull(null, " The default value is ")); // The default value is
5、 ... and 、 Object empty
10、 Determines if the object is null
// 10、 Determines if the object is null
// Support :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、 Judge whether the object is not empty
// 11、 Judge whether the object is not empty
// Support :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
6、 ... and 、 Get extreme values
12、 Get the maximum
// 12、 Get the maximum
System.out.println(ObjectUtils.max(1, 2, 3, 4, 5)); // 5
13、 Get the minimum
// 13、 Get the minimum
System.out.println(ObjectUtils.min(1, 2, 3, 4, 5)); // 1
14、 Get the median
// 14、 Get the median
System.out.println(ObjectUtils.median(1, 2, 3, 4, 5)); // 3
7、 ... and 、 Complete code
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
// One 、 Judge empty and non empty
// 1、 Determine whether the value of any element in a given array is not 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、 Determine whether the value of any element in a given array is null
System.out.println(ObjectUtils.allNull(null, null, null)); // true
System.out.println(ObjectUtils.allNull("a", null, null)); // false
// 3、 Determine whether the elements in a given array have no null Value
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、 Determine whether the elements in a given array have yes null Value
System.out.println(ObjectUtils.anyNull("a", "b", "c")); // false
System.out.println(ObjectUtils.anyNull(null, "b", "c")); // true
// Two 、 Clone objects - Shallow clone
// 5、 Clone objects - If the object implements Cloneable Clone , Otherwise null- Shallow clone
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);
// Shallow clone
// DogCloneable(name=zibo, age=2, son=DogCloneable(name=son2, age=1, son=null))
// 6、 Clone objects , If you return `null` , Returns the original object - Shallow clone
// First tune clone(final T obj) , If you return null, Return to the original obj object
Dog son1 = new Dog("son", 1, null);
Dog dog1 = new Dog("zibo", 2, son1);
Dog cloneIfPossible = ObjectUtils.cloneIfPossible(dog1);
System.out.println(cloneIfPossible);
// Unrealized `Cloneable` Interface , return `null` , Return to the original object
// Dog(name=zibo, age=2, son=Dog(name=son, age=1, son=null))
// 3、 ... and 、 Compare the size
// 7、 Compare the size
// grammar :int compare(T c1, T c2)
// The rules : If c1 < c2, Then return to -1 ; If c1 > c2, Then return to +1 ; If c1 = c2, Then return to 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、 Compare the size -null More valuable
// Note the difference from the above
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
// Four 、 by `null` The default value is
// 9、 If the object is null , Return default
System.out.println(ObjectUtils.defaultIfNull(null, " The default value is ")); // The default value is
// 5、 ... and 、 Object empty
// 10、 Determines if the object is null
// Support :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、 Judge whether the object is not empty
// Support :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
// 6、 ... and 、 Get extreme values
// 12、 Get the maximum
System.out.println(ObjectUtils.max(1, 2, 3, 4, 5)); // 5
// 13、 Get the minimum
System.out.println(ObjectUtils.min(1, 2, 3, 4, 5)); // 1
// 14、 Get the median
System.out.println(ObjectUtils.median(1, 2, 3, 4, 5)); // 3
// end
}
}
边栏推荐
- andriod6.0低功耗模式(关闭wifi、蓝牙、gps、屏幕亮度等)
- Execute immediate simple sample set (DML)
- In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again
- Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency
- 多线程顺序运行的几种方法,面试可以随便问
- Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
- MySQL的隔离级别、可能出现的问题(脏读、不可重复读、幻读)及其解决方法
- SDRAM控制器设计(数字控制器的两种设计方法)
- Common measurement matrix and matlab code of compressed sensing
- [basic course of flight control development 8] crazy shell · open source formation uav-i2c (laser ranging)
猜你喜欢

【AD学习】本次海上航行器大赛画pcb图的历程

Five interesting magic commands in jupyter notebook

追踪伦敦银实时行情的方法有哪些?

Yield Guild Games:这一年的总结与未来展望

AQS原理

新拟态个人引导页源码

状态压缩dp-蒙德里安的梦想

NFT 项目的 7 种市场营销策略

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(8)毕业设计论文模板

Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
随机推荐
会议OA项目之会议通知&会议反馈&反馈详情功能
What are the methods to track the real-time market of London Silver?
Hash table~
【Web开发】Flask框架基础知识
第二轮1000个Okaleido Tiger,再次登录Binance NFT 1小时售罄
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)
Api 接口优化的那些技巧
如何给女友讲明白JS的bind模拟实现
【commons-lang3专题】005- ObjectUtils 专题
华为发布HarmonyOS 3.0,向“万物互联”再迈一步
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(5)任务书
Jupyter notebook中5个有趣的魔法命令
用CDO进行nc数据的不规则裁剪
Android必备的面试技能(含面试题和学习资料)
Techo Hub 福州站干货来袭|与开发者共话工业智能新技术
COPU陆首群教授应邀在开放原子全球开源峰会上做主旨演讲
armeabi-v7a架构(sv7a)
【commons-lang3专题】004- NumberUtils 专题
Some considerations about ThreadPool
mysql存储过程 实现创建一张表(复制原表的结构新建的表)