java 将一个对象合并到另一个对象中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30782026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
merging one object into another object
提问by Deepak Kumar
I have two Objects of same type.I want to create a method which can merge the properties of two object and can return a single object.
我有两个相同类型的对象。我想创建一个可以合并两个对象的属性并可以返回单个对象的方法。
For Example: Consider a class ABC having 4 fields
例如:考虑一个有 4 个字段的 ABC 类
Class ABC{
String name;
String id;
String salary;
String status;
}
Suppose the first object is
假设第一个对象是
ABC[name=ZEE,id=2,salary=null,status=1]
and the second object is
第二个对象是
ABC[name=john,id=null,salary=12200,status=null]
I want to make a generic method which can merge these two objects and can give result output as:
我想创建一个可以合并这两个对象的通用方法,并且可以将结果输出为:
ABC[name=ZEE,id=2,salary=12200,status=1]
The method Should take two parameters of Object type:
该方法应该接受两个 Object 类型的参数:
Object mergeObject(Object Obj1, Object Obj2){
}
Note: It will take the first object property if both the objects have non-null value for that property.
注意:如果两个对象都具有该属性的非空值,它将采用第一个对象属性。
回答by EpicPandaForce
You're going to have to go the reflection route. I'm assuming you have a default constructor, otherwise the following won't work. Also, it needs two same types. It won't copy inherited fields, for that, you also need to add some code from here.
你将不得不走反射路线。我假设你有一个默认的构造函数,否则下面的将不起作用。此外,它需要两种相同的类型。它不会复制继承的字段,为此,您还需要从此处添加一些代码。
@SuppressWarnings("unchecked")
public static <T> T mergeObjects(T first, T second) throws IllegalAccessException, InstantiationException {
Class<?> clazz = first.getClass();
Field[] fields = clazz.getDeclaredFields();
Object returnValue = clazz.newInstance();
for (Field field : fields) {
field.setAccessible(true);
Object value1 = field.get(first);
Object value2 = field.get(second);
Object value = (value1 != null) ? value1 : value2;
field.set(returnValue, value);
}
return (T) returnValue;
}
Here's an example
这是一个例子
public static class ABC {
private int id;
private String name;
private int[] numbers;
public ABC() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int[] getNumbers() {
return numbers;
}
public void setNumbers(int[] numbers) {
this.numbers = numbers;
}
}
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
ABC abc = new ABC();
abc.setId(1);
abc.setName("Hello");
int[] newnumbers = new int[5];
for(int i = 0; i < newnumbers.length; i++) {
newnumbers[i] = i;
}
abc.setNumbers(newnumbers);
ABC abc2 = new ABC();
abc2.setName("World");
ABC abcFinal = mergeObjects(abc, abc2);
System.out.println("Properties of ABC Final:");
System.out.println("ID: " + abcFinal.getId());
System.out.println("Name: " + abcFinal.getName());
System.out.println("Numbers: " + Arrays.toString(abcFinal.getNumbers()));
}
Output:
输出:
Properties of ABC Final:
ID: 1
Name: Hello
Numbers: [0, 1, 2, 3, 4]
回答by dosw
In case you want to create a new Object use:
如果要创建新对象,请使用:
public ABC mergeABC(ABC obj1, ABC obj2){
ABC retVal = new ABC();
retVal.name = ((obj1.name != null) ? obj1.name : obj2.name );
retVal.id = ((obj1.id != null) ? obj1.id : obj2.id );
retVal.salary = ((obj1.salary != null) ? obj1.salary : obj2.salary );
retVal.status = ((obj1.status != null) ? obj1.status : obj2.status );
return retVal;
}
If you want to "reuse" obj1 or obj2, simply use this as return value.
如果您想“重用” obj1 或 obj2,只需将其用作返回值。
回答by Othya
If you want to prioritize the values in Object 1 (in case both objects have values), then follow something like this..
如果您想优先考虑对象 1 中的值(如果两个对象都有值),请按照以下步骤操作..
private CustomObject mergeObject(CustomObject Obj1, CustomObject Obj2) {
CustomObject tempObject = new CustomObject();
If (Obj1.getName() == null) {
tempObject.setName(Obj2.getName());
} else {
tempObject.setName(Obj1.getName());
}
return tempObject;
}
Iterate over the if statement for each parameter within the object.
为对象中的每个参数迭代 if 语句。
回答by Karrde
If you want to do it generically, your only option is going to be reflection.
如果你想笼统地做这件事,你唯一的选择就是反思。