java: Merging objects [closed]
I need to merge 2 objects into 1. Eg I have:
Object obj1=new Object();
Object obj2=new Object();
I need to merge obj1 and obj2 into 1.
Assuming Object1 and Object2 are a) of the same type and b) java bean compatible, you could use Commons / BeanUtils like this:
Map<String, Object> beanMap1 = BeanUtils.describe(object1);
Map<String, Object> beanMap2 = BeanUtils.describe(object2);
// now merge beanMap1 into beanMap2
Object merged = new YourCustomObject();
BeanUtils.populate(beanMap2, merged);
It is not clear what you mean by merge. You cannot just merge two objects. However, you can create a composite objects out of the two objects you have.
Why don't you create a third object:
Object thirdObject = new Object();
and extract the information you need from obj1
and obj2
and insert into thirdObject
?
精彩评论