开发者

java: Merging objects [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago. 开发者_开发问答

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜