当前位置:网站首页>BeanUtils. Copyproperties() and jsonobject What copy type is parseobject() (shallow copy or deep copy)
BeanUtils. Copyproperties() and jsonobject What copy type is parseobject() (shallow copy or deep copy)
2022-07-21 04:23:00 【Mingkuai de xuanmi 61】
Catalog
One 、 Conclusion
- BeanUtils.copyProperties(): Shallow copy
- JSONObject.parseObject(): Deep copy
Two 、 prove BeanUtils.copyProperties() Is a shallow copy
Code :
import org.springframework.beans.BeanUtils;
public class Test {
public static void main(String[] args) throws Exception {
Info info1 = new Info();
Student student = new Student();
student.setName(" Li Hua ");
student.setAge(18);
info1.setCountry(" China ");
info1.setStudent(student);
Info info2 = new Info();
BeanUtils.copyProperties(info1, info2);
info2.setCountry("American");
info2.getStudent().setName("Tom");
info2.getStudent().setAge(18);
System.out.println(info1);
System.out.println(info2);
}
}
class Info {
/** National Information **/
private String country;
/** Basic information of students **/
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Info{" +
"country='" + country + '\'' +
", student=" + student +
'}';
}
}
/** * Reference data type */
class Student {
/** full name **/
private String name;
/** Age **/
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
result :
Info{
country=' China ', student=Student{
name='Tom', age=18}}
Info{
country='American', student=Student{
name='Tom', age=18}}
explain :
You can see from the results student All are Tom, It means it's a shallow copy
expand :
Don't know much about BeanUtils.copyProperties()
Please see BeanUtils.copyProperties(A, B) Use
3、 ... and 、 prove JSONObject.parseObject() It's a deep copy
Code :
class Test {
public static void main(String[] args) throws Exception {
Info info1 = new Info();
Student student = new Student();
student.setName(" Li Hua ");
student.setAge(18);
info1.setCountry(" China ");
info1.setStudent(student);
Info info2 = JSONObject.parseObject(JSONObject.toJSONString(info1), new TypeReference<Info>(){
});
info2.setCountry("American");
info2.getStudent().setName("Tom");
info2.getStudent().setAge(18);
System.out.println(info1);
System.out.println(info2);
}
}
class Info {
/** National Information **/
private String country;
/** Basic information of students **/
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Info{" +
"country='" + country + '\'' +
", student=" + student +
'}';
}
}
/** * Reference data type */
class Student {
/** full name **/
private String name;
/** Age **/
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
result :
Info{
country=' China ', student=Student{
name=' Li Hua ', age=18}}
Info{
country='American', student=Student{
name='Tom', age=18}}
explain :
You can see from the results student The name of the object is different , The description is a deep copy
Four 、 summary
In the example Info Classes and properties Student Classes do not implement Cloneable Interface , No rewriting clone() Method , It doesn't make sense to say that they are shallow copies or deep copies , But the truth is similar , If you want to understand what is shallow copy , What is deep copy , Please watch Reference copy 、 Shallow copy 、 Deep copy
边栏推荐
- 启新聚势 云谱新篇|海泰方圆与四川联通达成生态战略合作
- RAID磁盘阵列
- STM32 learning (2) GPIO
- BeanUtils.copyProperties()和JSONObject.parseObject()是什么拷贝类型(浅拷贝或者深拷贝)
- Sword finger offer II 016 Longest substring without duplicate characters
- 文件系统与日志分析
- Understanding and using unity2d custom scriptable tiles (I) -- understanding the tilebase class
- What is rendering pipeline and how to draw 3D objects
- Uni app - solution to the failure of the return key of the mobile phone with the embedded web page physics in the app platform (after the embedded WebView web page H5 is packaged, the mobile phone's p
- JSON parsing
猜你喜欢
随机推荐
Application of safertos in medical devices
Qu'est - ce qu'un tuyau de rendu et comment dessiner un objet 3D
Shell循环语句详解--while、until循环
See "cocos2dx-3.0 as I understand it" combined with the source code - rendering process
The new version of WordPress 6.0.1 has been released, and it is recommended to update it all.
SNAT与DNAT
磁盘的配额
不知道 MySQL 咋学?刷完牛客这 50 道题就够了!(第六篇)
Combined with the source code, see "cocos2dx-3.0 as I understand" - particle system
构造方法,静态方法,抽象方法,接口中的默认方法
Process and planned task management
Understanding and use of unity2d custom scriptable tiles (II) -- understand the methods of the tilebase class (mainly gettiledata method) in detail by understanding the tile class
买一个能拍蒜的菜刀要多少钱?通过爬虫爬取菜刀价格
什么是FTP
About the road before and after graduation
Three swordsmen awk
Ugui official optimization document translation
See "cocos2dx-3.0 as I understand it" in combination with the source code - Overview
Rust安装并实现一个helloworld
rpm与yum的区别