I have nested lists and I'm trying to group and sum to get the desired result using java streams and collectors . With this I'm not able to loop over multiple SubAccounts
. Either I have to use for loop or some other logic. I want to achieve using streams api. Is there any possibility for that
Map<Long, BigDecimal> assetQuanMap = subAccounts.getAssets.parallelStream().collect(Collectors.groupingBy(Asset::getAssetId, Collectors.reducing(BigDecimal.ZERO, Asset::getQuantity, BigDecimal::add)));
I'm having the below classes or representation :
Account
SubAccount1
Assets
1 - 20
2 - 30
3 - 40
SubAccount2
Assets
1 - 10
2 - 5
3 - 3
SubAccount3
1 - 3
2 - 3
3 - 4
Accounts class looks like :
Public class Account{
List<SubAccounts> list;
}
Public Class SubAccounts {
List<Assets> list;
}
Public class Assets{
Long assetId;
BigDecimal quantity ;
}
I'm trying to get the result as below in Map . Basically for each of the subAccounts i need to group the assets at account level which looks similar to below
1 - 33
2 - 38
3 - 47