Home > Enterprise >  How to iterate a List of Model in java and update the value of some of model element in list
How to iterate a List of Model in java and update the value of some of model element in list

Time:10-26

I have a array list of a Model class which has multiple String type variables. This Array list values is populated from JDBC template result set.

Now I want to iterate this Array List and update some of these model element based upon some conditions.

My Model Class:

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

My DAO Class:

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

}

I want to iterate "claimDataList" and check whether plan_number is null and update subGroupPolicyNumber accordingly based upon the value of plan_number.

I can iterate List of model but don't know how to update the values in List of model. Please help me to update the values in "claimDataList"

CodePudding user response:

If I have understood, you want to check that the value 'PLAN_NUMBER' is not null, and then fill the variable 'SUBGRUOUP_POLICY_NUMBER'.

You can do this with lambdas (since j8), which is optimal.

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

CodePudding user response:

Write a method to update one model. Then you can iterate over the list of models, filter them and call this method for the resuming models. I would prefer a stream:

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

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

CodePudding user response:

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