Reputation: 580
My basic problem is that I have 4 different datasources(DS1, DS2, DS3,DS4) that I currently store in 4 different lists. Every list is filled with InformationObjects and these objects have four fields that are identifiers if they exist, but all of them does not have to exist. And then there is about 20 fields containing random information about the object.
What I then need to do is to merge these lists into a new one and if two objects from different datasources have the same of any of the identifiers they should be considered as the same object and their fields should be merged.
Example:
Object1
idField1: "123"
idField3: "437"
infoField1: "info1"
infoField2: "info2"
Object2
idField1: "123"
idField2: "gfd"
idField4: "9987"
infoField3: "info3"
infoField4: "info4"
Object3
idField2: "gfd"
infoField5: "info5"
Merged
idField1: "123"
idField2: "gfd"
idField3: "437"
idField4: "9987"
infoField1: "info1"
infoField2: "info2"
infoField3: "info3"
infoField4: "info4"
infoField5: "info5"
This merge will be done a lot so I need to find the fastest way to do it. So my question is how you can do this in the most efficient way?
Upvotes: 0
Views: 171
Reputation: 28951
List<InformationObject> doMerge()
{
Map<String, List<InformationObject>> map = new HashMap<>();
addData(map, ds1);
addData(map, ds2);
addData(map, ds3);
addData(map, ds4);
List<InformationObject> result = new ArrayList<>();
for(List<InformationObject> ios: map.values())
{
InformationObject io = mergeObjects(ios);
result.add(io);
}
return result;
}
private void addData(Map<String, List<InformationObject>> map, Datasource ds)
{
for(InformationObject io : ds...)
{
String id = io.getId();
List<InformationObject> list = map.get(id);
if(list == null) list = new ArrayList<>();
list.add(io);
}
}
Upvotes: 1