2 回答

TA貢獻1873條經驗 獲得超9個贊
您有兩個嵌套map操作。外部將 a 轉換contract為 a List<FeeAccount>,內部將 a 轉換Fee為 a FeeAccount。
因此,您的管道導致Stream<List<FeeAccount>>沒有終端操作。
如果你.collect(Collectors.toList())在最后添加一個,你會得到一個List<List<FeeAccount>>.
如果您想將所有這些內部列表合并到一個輸出列表中,您應該使用flatMap.
獲得單位List:
List<FeeAccount> feeAccounts =
contractList.stream()
.flatMap(contract -> {
List<Fee> monthlyFees=...;
return monthlyFees.stream()
.map(monthlyFee -> {
FeeAccount account = new FeeAccount();
account.setFeeCode(monthlyFee.getFeeCode());
account.setDebtorAccount(contract.getDebtorAccount());
return account;
});
})
.collect(Collectors.toList();

TA貢獻1765條經驗 獲得超5個贊
map()
是流管道中的中間操作(請參閱流操作和管道),這意味著它返回一個流。
feeAccounts = contractList
.stream()
.map(...) // result of this operation is Stream<<List<FeeAccount>>
and not a List<FeeAccount>
您缺少終端操作,例如.collect(Collectors.toList():
List<FeeAccount> feeAccounts = contractList
.stream()
.flatMap(monthlyFees -> monthlyFees.stream()
.map(monthlyFee -> {
FeeAccount account = new FeeAccount();
account.setFeeCode(monthlyFee.getFeeCode());
account.setDebtorAccount(contract.getDebtorAccount());
return account;
})
.collect(Collectors.toList());
flatMap轉換Stream<Stream<FeeAccount>>
為Stream<FeeAccount>
添加回答
舉報