Reputation: 31
I Hava two ArrayList of objects like
ArrayList<Record> dataSetOne;
ArrayList<Record> dataSetTwo;
Where Records objects looks like
public class Record{
private String id;
private String name;
private String HomeAdrress;
private String OfficeAdrress;
}
So, In first ArrayList i.e, dataSetOne i will get record data related home adress details.
public class Record{
id = emp01;
name = andy;
HomeAdrress = mexico;
}
In second ArrayList i.e, dataSetTwo i will get record data related office adress details.
public class Record{
id = emp01;
name = andy;
officeAdrress = california;
}
So the requitrement is i need to merge these two arraylist and combine records with id as primary and get both home and office address like below.
public class Record{
id = emp01;
name = andy;
HomeAdrress = mexico;
officeAdrress = california;
}
Thanks
Upvotes: -1
Views: 45
Reputation: 307
Record class must be :
class Record{
public int id;
public String name;
public String homeAddress;
public String officeAdrress;
}
and main function :
public static void main(String[] args) {
ArrayList<Record> merged = new ArrayList<>();
Record r;
for(int i = 0; i < dataSetOne.size(); i++) {
for(int j = 0; j < dataSetTwo.size(); j++){
if(dataSetOne.get(i).id == dataSetTwo.get(j).id){
r = new Record();
r.id = dataSetOne.get(i).id;
r.name=dataSetOne.get(i).name;
r.homeAddress = dataSetOne.get(i).homeAddress;
r.officeAdrress = dataSetTwo.get(j).officeAdrress;
merged.add(r);
}
}
}
}
Upvotes: 0
Reputation: 95
import java.util.*;
/* define your IncompleteRecordErr here */
class IncompleteRecordException extends Exception {
IncompleteRecordException() {
super("Your record is incomplete. Vital data missing!");
}
}
/* */
class Record {
public int id;
public String name;
public String homeAddress;
public String officeAdrress;
}
public class App {
public static void main(String[] args) {
ArrayList<Record> arrList1 = new ArrayList<>();
ArrayList<Record> arrList2 = new ArrayList<>();
/* full your arrLists above with elements */
ArrayList<Record> merged = new ArrayList<>();
for(int i = 0; i < arrList1.size(); i++) { // supposed both data lists have same len
Record r1 = arrList1.get(i);
Record r2 = arrList2.get(i);
Record currentRecord = new Record();
if(r1.id != 0) currentRecord.id = r1.id; // check for not default value
else if(r2.id != 0) currentRecord.id = r2.id;
//else throw new IncompleteRecordException();
if(r1.name != null) currentRecord.name = r1.name;
else if(r2.name != null) currentRecord.name = r2.name;
//else throw new IncompleteRecordException();
if(r1.homeAddress != null) currentRecord.homeAddress = r1.homeAddress;
else if(r2.homeAddress != null) currentRecord.homeAddress = r2.homeAddress;
//else throw new IncompleteRecordException();
if(r1.officeAdrress != null) currentRecord.officeAdrress = r1.officeAdrress;
else if(r2.officeAdrress != null) currentRecord.officeAdrress = r2.officeAdrress;
//else throw new IncompleteRecordException();
merged.add(currentRecord);
}
// now, "merged" should contain your merged data
}
}
Upvotes: 1