【发布时间】: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<String>?
标签: java list java-8 java-stream