Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grouping and sum with nested lists

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
like image 591
sk27 Avatar asked Mar 25 '19 17:03

sk27


People also ask

How do you find the sum of a nested list?

We can find sum of each column of the given nested list using zip function of python enclosing it within list comprehension. Another approach is to use map(). We apply the sum function to each element in a column and find sum of each column accordingly.

What are nested lists explain with example?

A nested list is a list that appears as an element in another list. In this list, the element with index 3 is a nested list. If we print( nested[3] ), we get [10, 20] . To extract an element from the nested list, we can proceed in two steps.

Can a list be nested in another list?

A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list. You can use them to arrange data into hierarchical structures.

What is a nested list?

A nested list is simply a list that occurs as an element of another list (which may of course itself be an element of another list, etc.). Common reasons nested lists arise are: They're matrices (a list of rows, where each row is itself a list, or a list of columns where each column is itself a list).


Video Answer


1 Answers

You have to use two flatMap so you can group by assetId

Map<String, BigDecimal> collect = accounts.stream()
        .flatMap(account -> account.getList().stream())
        .flatMap(subAccount -> subAccount.getList().stream())
        .collect(Collectors.groupingBy(Assets::getAssetId,
                Collectors.reducing(
                        BigDecimal.ZERO, 
                        Assets::getQuantity,
                        BigDecimal::add)
        ));

from your code assetId is a String so the key of map should be a String, or you have to convert it, or change it in your class, like so :

Map<Long, BigDecimal> collect = accounts.stream()
        .flatMap(account -> account.getList().stream())
        .flatMap(subAccount -> subAccount.getList().stream())
        .collect(Collectors.groupingBy(asset -> Long.valueOf(asset.getAssetId()),
                Collectors.reducing(
                        BigDecimal.ZERO,
                        Assets::getQuantity,
                        BigDecimal::add
                )
        ));

Notes

  • don't use plurals in the name of classes;
  • don't name variable as list, use another significant name;
  • don't use assetId as a name of attribute instead use just use id;
  • don't use List in the name of variables assetList, instead use s in the end for example assets or accounts.
like image 122
YCF_L Avatar answered Sep 20 '22 01:09

YCF_L