当前位置:网站首页>[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
}
}
边栏推荐
- Definition of double linked list~
- Wechat campus bathroom reservation for the finished product of applet graduation design (7) mid term inspection report
- What are the methods to track the real-time market of London Silver?
- 返回*this的成员函数
- Armeabi-v7a architecture (sv7a)
- 异步模式之工作线程
- 数仓搭建——DWT层
- DRF - paging, JWT introduction and principle, JWT quick use, JWT source code analysis, JWT custom return format, custom user issued token, custom token authentication class
- 【commons-lang3专题】004- NumberUtils 专题
- DRF -- authentication, authority, frequency source code analysis, global exception handling, automatic generation of interface documents, RBAC introduction
猜你喜欢

AQS principle

Data warehouse construction - DWT floor

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(5)任务书

ZABBIX deployment and monitoring

【目标检测】YOLOR理论简介+实践测试VisDrone数据集

Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency

深度学习 | MATLAB实现TCN时间卷积神经网络spatialDropoutLayer参数描述

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

Hash table~

Techo Hub 福州站干货来袭|与开发者共话工业智能新技术
随机推荐
Introduction of shortest path tree (SPT) and matlab code
Techo Hub 福州站干货来袭|与开发者共话工业智能新技术
DRF -- authentication, authority, frequency source code analysis, global exception handling, automatic generation of interface documents, RBAC introduction
浅谈一下跨端技术方案
主线程与守护线程
数学建模及其基础知识详解(化学常考知识点)
What are the methods to track the real-time market of London Silver?
SDRAM控制器设计(数字控制器的两种设计方法)
【commons-lang3专题】004- NumberUtils 专题
Flask sends verification code in combination with Ronglian cloud
zabbix部署及监控
华为发布HarmonyOS 3.0,向“万物互联”再迈一步
DRF - paging, JWT introduction and principle, JWT quick use, JWT source code analysis, JWT custom return format, custom user issued token, custom token authentication class
Selenium wire obtains Baidu Index
追踪伦敦银实时行情的方法有哪些?
andriod6.0低功耗模式(关闭wifi、蓝牙、gps、屏幕亮度等)
【无标题】
UE4 common printing information methods for debugging
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告
面试突击69:TCP 可靠吗?为什么?