I have this master and detail object to store header and detail list:
public class MasterDetail {
private Master master;
private List<Detail> details;
}
Now I am trying to create a unique list where master - trx date is distinct.
Example input list:
1 | 22/11/2012 | 10.00
2 | 22/11/2012 | 10.00
3 | 23/11/2012 | 11.00
4 | 23/11/2012 | 12.00
The out I needed:
Master1: 22/11/2012
Detail1: 10.00
10.00
Master2: 23/11/2012
Detail2: 11.00
12.00
I tried:
List<MasterDetail > masterDetails = new ArrayList<>();
for(Input input: inputList) {
// this is where to assign the input master and detail into an object
master = new Master(input.getTransactionDate);
details = new Detail(input.getAmount);
MasterDetail assign = new MasterDetail(master, details);
if (!masterDetails.isEmpty()) {
for (int i=0; i < masterDetails.size(); i++) {
if (masterDetails .get(i).getMaster().getTransDate().equals(input.getTransactionDate())) {
masterDetails.set(i, assign); // I think the logic problem here
} else {
masterDetails.add(assign );
}
};
} else {
masterDetails.add(assign)
}
}
The above code will give many duplicate records.
inputfor construction of you MasterDetail.masteranddetaildo not change valueMap<Date, List<Details>>and it will be much more efficient since you won't have to iterate on all themaster-details on each input you want to add.