如何用Java 8 Stream结合已有属性,根据列表生成多个Account对象?
解决方案:基于列表生成包含公共属性的多个Account对象
问题根源
现有代码仅为Account对象赋值了linkedDetails中的属性,未将AccountRequest中的公共属性(accountId、custId、custName、comments、status)传递给每个生成的Account,导致这些字段值为null。
修正代码(基础实现)
先提取AccountRequest中的公共属性,再遍历linkedDetails为每个Account对象同时设置公共属性和关联详情属性:
List<Account> accountList = new ArrayList<>(); List<AccountRequest> accountRequestList = request.getAccountRequest(); // 遍历所有AccountRequest(兼容多个请求的场景) for (AccountRequest accountRequest : accountRequestList) { // 提取公共属性,避免重复调用getter String accountId = accountRequest.getAccountId(); String custId = accountRequest.getCustId(); String custName = accountRequest.getCustName(); String comments = accountRequest.getComments(); String status = accountRequest.getStatus(); // 遍历关联详情生成Account对象 accountRequest.getLinkedDetails().forEach(linkedDetails -> { Account account = new Account(); // 设置公共属性 account.setAccountId(accountId); account.setCustId(custId); account.setCustName(custName); account.setComments(comments); account.setStatus(status); // 设置关联详情属性 account.setCustCode(linkedDetails.getCustCode()); account.setStartOn(linkedDetails.getStartOn()); account.setLoanType(linkedDetails.getLoanType()); accountList.add(account); }); }
优化实现(Builder模式)
为Account类添加Builder模式,让对象创建更简洁易读,同时避免重复的setter调用:
第一步:修改Account类添加Builder
class Account{ @JsonProperty("accountId") private String accountId; // 注意:示例请求中accountId是字符串类型,需修正原Long类型为String,否则反序列化失败 @JsonProperty("custId") private String custId; private String custName; private String comments; private String status; private String custCode; private LocalDate startOn; private String loanType; // Builder内部类 public static class Builder { private String accountId; private String custId; private String custName; private String comments; private String status; private String custCode; private LocalDate startOn; private String loanType; public Builder accountId(String accountId) { this.accountId = accountId; return this; } public Builder custId(String custId) { this.custId = custId; return this; } public Builder custName(String custName) { this.custName = custName; return this; } public Builder comments(String comments) { this.comments = comments; return this; } public Builder status(String status) { this.status = status; return this; } public Builder custCode(String custCode) { this.custCode = custCode; return this; } public Builder startOn(LocalDate startOn) { this.startOn = startOn; return this; } public Builder loanType(String loanType) { this.loanType = loanType; return this; } public Account build() { Account account = new Account(); account.accountId = this.accountId; account.custId = this.custId; account.custName = this.custName; account.comments = this.comments; account.status = this.status; account.custCode = this.custCode; account.startOn = this.startOn; account.loanType = this.loanType; return account; } } // 需保留Getter和Setter方法,或使用Lombok的@Data注解简化 }
第二步:使用Builder生成Account对象
List<Account> accountList = new ArrayList<>(); List<AccountRequest> accountRequestList = request.getAccountRequest(); for (AccountRequest accountRequest : accountRequestList) { accountRequest.getLinkedDetails().forEach(linkedDetails -> { Account account = new Account.Builder() .accountId(accountRequest.getAccountId()) .custId(accountRequest.getCustId()) .custName(accountRequest.getCustName()) .comments(accountRequest.getComments()) .status(accountRequest.getStatus()) .custCode(linkedDetails.getCustCode()) .startOn(linkedDetails.getStartOn()) .loanType(linkedDetails.getLoanType()) .build(); accountList.add(account); }); }
关键注意点
- 类型匹配:原
Account类中accountId定义为Long,但示例请求中是字符串"10EIIP",会导致反序列化失败,需将accountId类型改为String,或确保请求中的accountId为数字类型。 - 多请求兼容:代码处理了
accountRequestList中的所有元素,而非仅取第一个,适配多个请求的场景。
内容的提问来源于stack exchange,提问作者user8401182
相关产品推荐
相关产品推荐

