0
transactionId|age|amount|accType|
0000000000001|1  |1.0000|saving |
0000000000002|1  |1.0000|saving |
0000000000003|1  |1.0000|current|

How do I loop and group them by acc type using stream?

I want to have a map like below:

saving: [{0000000000001|1.0000|saving}, {0000000000002|1.0000|saving}]
current: [{0000000000003|1.0000|current}]

Currently I am doing the filtering without grouping:

txs.stream().filter(x -> x.getAge().equal(1)).filter(x -> x.getAmount().equals(1.0000)).collect(Collectors.toList());

I am trying:

Map<String, List<TX>> tx = txs.stream()
                                .collect(Collectors.groupingBy(p -> p.getAccountType()), Collectors.filtering(?))

How do I do multiple fitering?

Alvin
  • 8,219
  • 25
  • 96
  • 177
  • 2
    Why not filter before collecting? `Map> tx = txs.stream() .filter(x -> x.getAge().equal(1)) .filter(x -> x.getAmount().equals(1.0000)) .collect(Collectors.groupingBy(p -> p.getAccountType()));` – gkns Dec 20 '21 at 06:06
  • 1
    Also, getAmount().equals(1.00) seems not very accurate. You should probably consider using BigDecimal instead of floats (with apt. rounding) for the accuracy of currency handling. – gkns Dec 20 '21 at 06:08

0 Answers0