【问题标题】:Call a method for each element of a list using Streams使用 Streams 为列表的每个元素调用方法
【发布时间】:2020-07-05 09:53:54
【问题描述】:

我有一个Customer 类如下:

import java.util.List;

public class Customer {
    String customerId;
    String customerName;
    String primaryAccount;
    List<String> secondaryAccount;

    public Customer(String customerId, String customerName, String primaryAccount, List<String> secondaryAccount) {
        this.customerId = customerId;
        this.customerName = customerName;
        this.primaryAccount = primaryAccount;
        this.secondaryAccount = secondaryAccount;
    }

    public String getCustomerId() {
        return customerId;
    }

    public String getCustomerName() {
        return customerName;
    }

    public String getPrimaryAccount() {
        return primaryAccount;
    }

    public List<String> getSecondaryAccount() {
        return secondaryAccount;
    }
}

还有一个ManageCustomer 类:

import java.util.ArrayList;
import java.util.List;

public class ManageCustomer {

    public static void main(String[] args) throws InterruptedException {
        List<Customer> customers = new ArrayList<>();


        List<String> secondaryAccount = new ArrayList<>();
        secondaryAccount.add("savings");
        secondaryAccount.add("loanAccount");

        Customer harry = new Customer("123", "Harry",
                "checking", null);
        Customer jiya = new Customer("444", "Jiya",
                "checking", null );
        Customer rob = new Customer("333", "Rob",
                "checking", secondaryAccount);

        customers.add(harry);
        customers.add(jiya);
        customers.add(rob);

    }


    private List<String> addCustomerAccountDetails(String customeName, String primaryAccount, String SecondaryAccount) {

      // logic is here

    }
}

我想为客户列表的每个primaryAccount 和secondary account 调用addCustomerAccountDetails 方法。

所以,基本上我想要的输出是方法应该像这样总共调用 5 次

addCustomerAccountDetails("Harry", "checking", null);
addCustomerAccountDetails("Jiya", "checking", null);
addCustomerAccountDetails("Rob", "checking", null);
addCustomerAccountDetails("Rob", "checking", "savings");
addCustomerAccountDetails("Rob", "checking","loanAccount");

如何使用 Java8 流实现对客户列表的迭代?

【问题讨论】:

  • @Eklavya 这不是一个好问题,它是关于收藏家的......
  • @Eklavya 我认为嵌套和处理null 是这里的主要问题
  • 您为什么期望调用addCustomerAccountDetails("Rob", "checking", null);? “Rob”有不是null 的辅助帐户,那么呢?您是否打算忽略方法addCustomerAccountDetails 的返回类型List&lt;String&gt;

标签: java list java-8 java-stream


【解决方案1】:

你不需要流,List 提供了forEach 方法:

customers.forEach(c -> Optional
    .ofNullable(c.getSecondaryAccount())
    .map(l -> {
        l.add(null);
        return l;
    })
    .orElse(Collections.singletonList(null))
    .forEach(sa -> 
        addCustomerAccountDetails(
            c.getCustomerName(),
            c.getPrimaryAccount(),
            sa
    )
);
null。

【讨论】:

  • 只是好奇,我可以在这里使用流吗?
  • @Andronicus 鉴于辅助帐户是List,它应该是Collections.singletonList(null)。但这是对 Optional API 的滥用。 null 支票会更好。
  • 当然可以,我刚刚检查了我的 shell,谢谢 Naman
  • @Andronicus,谢谢。但解决方案不包括这种情况 addCustomerAccountDetails("Rob", "checking", null);
  • @codemaster001 那么您可能也想质疑这些要求,并不是我否认它不可能构建它,而是我怀疑您在这种情况下正在处理有缺陷的要求,这可能在分析过程中导致模棱两可的结果。例如如果我只看addCustomerAccountDetails("Jiya", "checking", null); addCustomerAccountDetails("Rob", "checking", null);,那么我将永远无法确定他们中的哪些人有辅助账户,哪些没有。
【解决方案2】:

引入了一个请求包装类

@AllArgsConstructor
class CustomerAccountDetailsRequest {
    String customerName;
    String primaryAccount;
    String secondaryAccount;
}

并更改方法签名,例如:

List<String> addCustomerAccountDetails(CustomerAccountDetailsRequest customerAccountDetailsRequest) {
    return new ArrayList<>(); // your implementation
}

您可以通过客户Stream 实现此目的:

customers.stream()
        .flatMap(customer -> customer.getSecondaryAccount() == null ?
                Stream.of(new CustomerAccountDetailsRequest(customer.getCustomerName(),
                        customer.getPrimaryAccount(), null)) :
                customer.getSecondaryAccount().stream()
                        .map(secondary -> new CustomerAccountDetailsRequest(
                                customer.getCustomerName(), customer.getPrimaryAccount(), secondary)))
        .map(request -> addCustomerAccountDetails(request)) // ensure invoking the method desired number of times
        .forEach(//do something with the result)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2016-01-24
    • 1970-01-01
    • 2021-01-13
    相关资源
    最近更新 更多