使用循环创建唯一列表



我有这个master和detail对象来存储header和detail列表:

public class MasterDetail {
private Master master;
private List<Detail> details;
}

现在我正在尝试创建一个唯一的列表,其中master - trx日期是不同的。

输入列表示例:

1 | 22/11/2012 | 10.00
2 | 22/11/2012 | 10.00
3 | 23/11/2012 | 11.00
4 | 23/11/2012 | 12.00

我需要的out:

Master1: 22/11/2012
Detail1: 10.00
10.00
Master2: 23/11/2012
Detail2: 11.00
12.00

我试着:

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)
}
}

上面的代码会产生许多重复的记录。

我认为一个很好的改进是使用Map<TransDate, MasterDetail>来帮助您根据所讨论的Input的日期有效地检索MasterDetail

但是,我不知道如何存储日期的细节,也不知道这是否很容易作为地图的键。如果您出于某种原因想坚持使用List数据结构,我会这样做:

List<MasterDetail> masterDetails = new ArrayList<>();
for(Input input: inputList) {
// find if there's a MasterDetail with the date in question
MasterDetail target = null;
for(MasterDetail mDetail : masterDetails){
if(mDetail.getMaster().getTransDate().equals(input.getTransactionDate()) {
// we found it
target = mDetail;
break;
}
if(target == null) {  // never found it, so make a new one
target = new MasterDetail(input.master, input.detail); // however you create one
masterDetails.add(target);
}
target.details.add(detail);
}

还有一些细节我不知道你的代码,但至少这个逻辑应该使它,所以只有一个MasterDetail为每个日期。

试试这个

record Input(LocalDate transactionDate, double amount) { }
record Master(LocalDate transDate) { }
record Detail(double amount) { }
record MasterDetail(Master master, List<Detail> details) { }
public static void main(String[] args) {
List<Input> inputList = List.of(
new Input(LocalDate.of(2012, 11, 22), 10.0),
new Input(LocalDate.of(2012, 11, 22), 10.0),
new Input(LocalDate.of(2012, 11, 23), 11.0),
new Input(LocalDate.of(2012, 11, 23), 12.0));

Map<LocalDate, List<Detail>> map = inputList.stream()
.collect(Collectors.groupingBy(Input::transactionDate,
Collectors.mapping(e -> new Detail(e.amount()), Collectors.toList())));
List<MasterDetail> masterDetails = map.entrySet().stream()
.map(e -> new MasterDetail(new Master(e.getKey()), e.getValue()))
.toList();
for (MasterDetail e : masterDetails)
System.out.println(e);
}

输出:

MasterDetail[master=Master[transDate=2012-11-22], details=[Detail[amount=10.0], Detail[amount=10.0]]]
MasterDetail[master=Master[transDate=2012-11-23], details=[Detail[amount=11.0], Detail[amount=12.0]]]

或者你也可以通过for循环来实现。

Map<LocalDate, List<Detail>> map = new HashMap<>();
for (Input input : inputList)
map.computeIfAbsent(input.transactionDate(),
k -> new ArrayList<>()).add(new Detail(input.amount()));
List<MasterDetail> masterDetails = new ArrayList<>();
for (Entry<LocalDate, List<Detail>> entry : map.entrySet())
masterDetails.add(new MasterDetail(new Master(entry.getKey()), entry.getValue()));

最新更新