2

我正在尝试使用 Java-8 lambdas 来解决以下问题:

给定 a List<Transaction>,对于每个,Category.minorCategory我需要Transaction.amountperCategory.minorCategory和 a MapofTransaction.accountNumber的总和以及Transaction.amountper的总和Transaction.accountNumber。根据下面的代码,我有这个工作。我现在需要分组Category.majorCategory,基本上返回一个Map<String, Map<String, MinorCategorySummary>>键控Category.majorCategory

我已经完成了所有工作,直到分组阶段,Category.majorCategory但很难找到解决方案;使用 lambda 进行编程的范式转变证明了一条陡峭的学习曲线。

TransactionBreakdown是动作发生的地方,我想返回一个Map<String, Map<String, MinorCategorySummary>>.

public class Transaction {

    private final String accountNumber;
    private final BigDecimal amount;
    private final Category category;

}

public class Category {

    private final String majorCategory;
    private final String minorCategory;

}

public class MinorCategorySummary {

    private final BigDecimal sumOfAmountPerMinorCategory;
    private final Map<String, BigDecimal> accountNumberSumOfAmountMap;
    private final Category category;

}

public class TransactionBreakdown {

    Function<Entry<String, List<Transaction>>, MinorCategorySummary> transactionSummariser = new TransactionSummariser();

    public Map<Object, MinorCategorySummary> getTransactionSummaries(List<Transaction> transactions) {
        return transactions
                .stream()
                .collect(groupingBy(t -> t.getCategory().getMinorCategory()))
                .entrySet()
                .stream()
                .collect(
                        toMap(Entry::getKey,
                                transactionSummariser));
    }

}

public class TransactionSummariser implements Function<Entry<String, List<Transaction>>, MinorCategorySummary> {

    @Override
    public MinorCategorySummary apply(
            Entry<String, List<Transaction>> entry) {
         return new MinorCategorySummary(
                    entry.getValue()
                            .stream()
                            .map(Transaction::getAmount)
                            .collect(reducing(BigDecimal.ZERO, BigDecimal::add)),
                    entry.getValue()
                            .stream()
                            .collect(
                                    groupingBy(Transaction::getAccountNumber,
                                            mapping(Transaction::getAmount,
                                                    reducing(BigDecimal.ZERO, BigDecimal::add)))), 
                                                    entry.getValue().get(0).getCategory());
    }

}
4

1 回答 1

5

你的班级设计对我来说似乎很奇怪。为什么只将类别放入摘要类中,然后将类别作为映射键?有一个没有类别的摘要类会更有意义:

public class TransactionSummary {

    private final BigDecimal amount;
    private final Map<String, BigDecimal> acctToTotal;

    TransactionSummary(Map<String, BigDecimal> acctToTotal) {
        this.acctToTotal = Collections.unmodifiableMap(acctToTotal);
        this.amount = acctToTotal.values().stream()
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    public static Collector<Transaction, ?, TransactionSummary> collector() {
        // this can be a static constant
        return collectingAndThen(
            toMap(Transaction::getAccountNumber,Transaction::getAmount,BigDecimal::add),
            TransactionSummary::new
        );
    }

    // getters
}

现在您的两个问题都得到了清晰的解决,没有冗余:

Map<String, TransactionSummary> minorSummary = transactions.stream()
        .collect(groupingBy(
                t -> t.getCategory().getMinorCategory(),
                TransactionSummary.collector()
        ));

Map<String, Map<String, TransactionSummary>> majorMinorSummary = transactions.stream()
        .collect(groupingBy(
                t -> t.getCategory().getMajorCategory(),
                groupingBy(
                        t -> t.getCategory().getMinorCategory(),
                        TransactionSummary.collector()
                )
        ));
于 2015-06-13T23:43:19.950 回答