简体   繁体   中英

Call a method for each element of a list using Streams

I have a Customer class as follows:

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:

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.

so, basically the output that what I want is the method should be called total 5 times like this

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?

You don't need streams for that, List provides forEach method:

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 . However, the cleaner solution would be to return an empty list instead of null .

With an introduction of a request wrapper 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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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