java - Merge multiple list of different Objects with a common field into another list -
i trying merge multiple list of objects common field list.
for example: list< obja>, list< objb>, list< objc>, list< objd>
obja : string id; string name, string a; string b;
objb : string id; string address; string x;
objc : string id; string phone; string y;
objd : string id; string zipcode;
now merge these lists list< objz> in efficient way.
objz: string id; string name, string a; string b; string address; string x; string phone; string y; string zipcode;
can please me in writing efficient code above?
use map instead of list, , make objs implement interface defines getid() use key. each entry in list a-d (and possibly create objz) map , update details.
you make quite generic. java pseudocode:
interface objcopier { string getid(); void copytoobjz(objz z); }
e.g.
class obja implements objcopier { ... string getid() { return id; } void copytoobjz(objz z) { z.setname(name); z.seta(a); ... } }
and final bit merge all:
public void mergeobj(map<string, objz> map, collection<? extends objcopier> list) { (objcopier obj : list) { objz z = getorcreateobjzfrommap(map, obj.getid()); obj.copytoobjz(z); } }
and call mergeobj() against lists have.
if you're talking extremely large data set, , there no overlap in fields across various obja-objd, consider using thread pool each of mergeobj() methods run in different thread. you'd need synchronize getorcreateobjzfrommap(), or pre-create objz instances before starting threads. speed process, mileage may vary best test data reflects situation.
Comments
Post a Comment