返回可选 而不是可选 在里面

问题描述 投票:1回答:3

我正在尝试编写一个返回最富有员工的流。

Optional<User> getRichestEmployee() {
    return getUserStream()
        .flatMap(user -> user.getAccounts().stream())
        .map(this::getAccountAmountInPLN)
        .sorted(Comparator.reverseOrder())
        .findFirst();
}

我的方法getAccounts()返回List<Account>

我的方法getAccountAmountInPLN看起来像这样:

BigDecimal getAccountAmountInPLN(final Account account) {
    return account
            .getAmount()
            .multiply(BigDecimal.valueOf(account.getCurrency().rate))
            .round(new MathContext(4, RoundingMode.HALF_UP));
}

我的问题是,getRichestEmployee()返回Optional<BigDecimal>

我无法让return成为最富有的员工。在流上运行时,我无法访问User对象。如何退回用户?

java java-8 functional-programming java-stream optional
3个回答
2
投票

首先,要找到最富有的员工,您需要将员工账户的金额相加。

其次,要找到总金额最大的员工,请使用max(Comparator<? super T> comparator)

例:

Optional<User> getRichestEmployee() {
    return getUserStream()
            .max(Comparator.comparing(this::getEmployeeAmountInPLN));
}

BigDecimal getEmployeeAmountInPLN(final User user) {
    return user.getAccounts()
            .stream()
            .map(this::getAccountAmountInPLN)
            .reduce(BigDecimal.ZERO, BigDecimal::add);
}

BigDecimal getAccountAmountInPLN(final Account account) {
    return account
            .getAmount()
            .multiply(BigDecimal.valueOf(account.getCurrency().rate))
            .round(new MathContext(4, RoundingMode.HALF_UP));
}

3
投票

我假设您通过查找金额最高的帐户来计算用户的财富。

首先创建一个额外的方法来从用户获取金额:

public BigDecimal getUserMaxAmount(User user) {
    return user
            .getAccounts()
            .stream()
            .map(this::getAccountAmountInPLN)
            .max(Comparator.naturalOrder())
            .orElse(BigDecimal.ZERO); //if user has no account I default to 0
}

然后你可以像这样使用它:

Optional<User> getRichestEmployee() {
    return getUserStream()
            .sorted(Comparator.comparing(this::getUserMaxAmount, Comparator.reverseOrder()))
            .findFirst();
}

甚至更简单:

Optional<User> getRichestEmployee() {
    return getUserStream().max(Comparator.comparing(this::getUserMaxAmount));
}

如果您的意图是通过汇总所有金额来计算用户的财富,您应该通过汇总金额将您的流减少到单个值:

public BigDecimal getUserTotalAmount(User user) { //instead of getUserMaxAmount
    return user
            .getAccounts()
            .stream()
            .map(this::getAccountAmountInPLN)
            .reduce(BigDecimal.ZERO, BigDecimal::add);

-1
投票

制作自己的比较器,不要将每个帐户映射到其余额:

Optional<User> getRichestEmployee() {
 return getUserStream()
    .flatMap(user -> user.getAccounts().stream())
    .sorted((a1, a2) -> this.getAccountAmountInPLN(a2).compareTo(this.getAccountAmountInPLN(a1)))
    // Assuming there's a getter for the account owner...
    .map(Account::getOwner) // replace with Account's getter for owner
    .findFirst();
© www.soinside.com 2019 - 2024. All rights reserved.