[英]Copy one Java object to another
What's the best way to copy one Java object of a class to another object of the same class?将一个类的一个 Java 对象复制到同一个类的另一个对象的最佳方法是什么? I tried BeanUtil.copyProperties but it didn't work for some reason.
我试过 BeanUtil.copyProperties 但由于某种原因它不起作用。 The class is a complex class.
该类是一个复杂的类。 (class contains another class objects etc)
(类包含另一个类对象等)
My aim is to populate values in order through hibernate function我的目标是通过休眠功能按顺序填充值
Public Response getOrder(Order order, Ticket ticket) {
order = OrderManager.getOrderByTicket(ticket); //Hibernate function This doesn't work, order object gets a new reference
}
Tried doing this试过这样做
Public Response getOrder(Order order, Ticket ticket) {
Order temp = OrderManager.getOrderbByTicket(ticket);
//Now I want to copy temp to order
}
If all the fields are serializable then you can use ObjectOutputStream
and ObjectInputStream
.如果所有字段都是可序列化的,那么您可以使用
ObjectOutputStream
和ObjectInputStream
。
If You need special handling during the serialization and deserialization process then implement special methods writeObject()
and readObject()
.如果在序列化和反序列化过程中需要特殊处理,则实现特殊方法
writeObject()
和readObject()
。
Please have a look at IO: Custom Reading and Writing with Serializable .请查看IO: Custom Reading and Writing with Serializable 。
Sample code:示例代码:
class MyClass implements Serializable {
private static final long serialVersionUID = 1L;
String str;
List<Integer> list = new ArrayList<Integer>();
public MyClass(String str) {
this.str = str;
}
}
MyClass obj1 = new MyClass("abc");
obj1.list.add(1);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(buffer);
oos.writeObject(obj1);
oos.close();
byte[] rawData = buffer.toByteArray();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(rawData));
MyClass obj2 = (MyClass) ois.readObject();
System.out.println(obj2.str);
System.out.println(obj2.list.get(0));
To do a deep copy using Serialize / DeSerialize, you can use the code like below,要使用 Serialize / DeSerialize 进行深度复制,您可以使用如下代码,
public Object deepCopy(Object input) {
Object output = null;
try {
// Writes the object
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(input);
// Reads the object
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
output = objectInputStream.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
I suppose you could use reflection if it was REALLY important or time consuming, but as I see it, there are two main choices我想如果反射真的很重要或很耗时,你可以使用它,但在我看来,有两个主要选择
One If you have access to the class, just implement Clonable
and have the clone
method produce a deep copy of the object and all its subobjects.一如果您有权访问该类,只需实现
Clonable
并让clone
方法生成对象及其所有子对象的深层副本。
Two Code it by hand.二 手工编码。 It may be time consuming and boring, but it works in all cases.
这可能既费时又乏味,但它适用于所有情况。
I believe you are talking about a 'deep' copy.我相信你在谈论一个“深”的副本。
A similar question and various solutions are detailed here:这里详细介绍了一个类似的问题和各种解决方案:
How do you make a deep copy of an object in Java? 你如何在 Java 中制作对象的深层副本?
The easiest way seems to be serialising the object and then deserialising it.最简单的方法似乎是序列化对象然后反序列化它。
Better do it like this:最好这样做:
Public Response getOrder(Request request) {
Order temp = OrderManager.getOrderbByTicket(request.getTicket());
request.setOrder(temp);
//process the response
}
This will solve the problem of getting back the Order to the caller of the function.这将解决将订单返回给函数调用者的问题。 If you want that the caller gets a deep copy than serialize and deserialize it before seting it to the request
如果您希望调用者在将其设置为请求之前获得深度副本而不是序列化和反序列化它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.