spring 如何在java中迭代模型列表并更新列表中某些模型元素的值

ma8fv8wu  于 2022-10-30  发布在  Spring
关注(0)|答案(3)|浏览(142)

我有一个Model类的数组列表,其中包含多个String类型变量。此数组列表值是从JDBC模板结果集填充的。
现在我想迭代这个数组列表,并根据一些条件更新其中的一些模型元素。
我的模型类:

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter

public class WADataModel {

public String STATUS;   
public String AUTO_DATE;
public String RECORD_TYPE;
public String VENDOR_NAME;
public String CREATED_DATE;
public String ACTION_CODE;
public String CITY;
public String GROUP_NUMBER;
public String GROUP_POLICY_NUMBER;
public String SUBGROUP_NUMBER;
public String SUBGROUP_POLICY_NUMBER;
public String SYSTEM;
public String PLAN_NUMBER;
}

我的DAO类别:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class PlanCrosswalks {

@Autowired
@Qualifier("nemisNamedJdbcTemplate")
private NamedParameterJdbcTemplate nemisJdbcTemplate;   

@Autowired
private FetchDatafFromProp fetchDatafFromProp;

@Value("${query.state}")
private String queryState;

public List<WADataModel> doWACrosswalk(List<WADataModel> claimDataList) throws ApplicationExceptions{

    RowMapper<WACrosswalkModel> rowMapper = new BeanPropertyRowMapper<>(WACrosswalkModel.class);
    List<WACrosswalkModel> statusResult = new ArrayList<>();
    Map<String,String> crosswalkQueryMap = new HashMap<>();
    Map<String,String> paramMap = new HashMap<>();
    crosswalkQueryMap = fetchDatafFromProp.getDataForGeneration();

    statusResult = nemisJdbcTemplate.query(crosswalkQueryMap.get(queryState+ Constants.UNDERSCORE + Constants.FETCH_SUB_GROUP_POLICY),paramMap,rowMapper);

    for(WADataModel model : claimDataList)
        //Here I want to update ClaimDataList elements like SUBGROUP_POLICY_NUMBER,GROUP_POLICY_NUMBER based upon some conditions by iterating whole List.

    return claimDataList; 
}

}
我想迭代“claimDataList”,检查plan_number是否为空,并根据plan_number的值相应地更新subGroupPolicyNumber。
我可以迭代模型列表,但不知道如何更新模型列表中的值。请帮助我更新“claimDataList”中的值

pqwbnv8z

pqwbnv8z1#

编写一个方法来更新一个模型。然后你可以遍历模型列表,过滤它们,并调用这个方法来恢复模型。我更喜欢流:

claimDataList.stream()
    .filter(model -> model.PLAN_NUMBER == null)
    .forEach(this::planNumberNull);

private void planNumberNull(WADataModel model) {
    model.SUBGROUP_POLICY_NUMBER = ...
}
ia2d9nvy

ia2d9nvy2#

for(WADataModel model : claimDataList){
  if(model.PLAN_NUMBER==null){
    model.SUBGROUP_POLICY_NUMBER = <>
  }
}
cl25kdpy

cl25kdpy3#

如果我已经理解了,你要检查值'PLAN_NUMBER'是否不为空,然后填充变量'SUBGRUUP_POLICY_NUMBER'。
您可以使用lambdas(从j8开始)来实现这一点,这是最优的。

claimDataList.stream()
.filter(x -> x.getPLAN_NUMBER() != null)
.forEach(y -> y.setSUBGROUP_POLICY_NUMBER(y.getPLAN_NUMBER()));

相关问题