简体   繁体   English

使用 Streams 为列表的每个元素调用方法

[英]Call a method for each element of a list using Streams

I have a Customer class as follows:我有一个Customer class 如下:

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;
    }
}

and there is a ManageCustomer class:并且有一个ManageCustomer class:

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

    }
}

I want to call the method addCustomerAccountDetails for every primaryAccount and secondary account of the customer list.我想为客户列表的每个主要帐户和辅助帐户调用方法 addCustomerAccountDetails。

so, basically the output that what I want is the method should be called total 5 times like this所以,基本上我想要的 output 方法应该像这样调用总共 5 次

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

How can I achieve this iterating over the customer list using Java8 streams?如何使用 Java8 流实现对客户列表的迭代?

You don't need streams for that, List provides forEach method:你不需要流, 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
    )
);

PS: I have used Optional with a singleton list with null element as default to handle the case, when the list is null . PS:当列表为null时,我已将Optional与 singleton 列表和null元素一起使用作为默认处理案例。 However, the cleaner solution would be to return an empty list instead of null .但是,更简洁的解决方案是返回一个空列表而不是null

With an introduction of a request wrapper class引入了请求包装器 class

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

and change in the method signature such as:并更改方法签名,例如:

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

you can achieve this with customer Stream as:您可以通过客户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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM