以优化方式合并两个列表

时间:2017-05-18 09:25:58

标签: java

我有两个清单。

List<Customer> customers1 = getCustomerDetails1();
List<Customer> customers2 = getCustomerDetails2();

public class Customer {
    private String firstName;
    private String lastName;
    private String gender;
    private String birthDate;
    private String formatedDob;
    private String mobileNumber;
    private String oldMobileNumber;
    private String emailId;
    private String add1;
    private String add2;
    private String add3;
    private String pincode;
    private String city;
    private String cityId;

    ----
    getter, setter

我是否正在合并两个列表数据如下:

List<Customer> customerVoList = new ArrayList<>();

for (Customer localCustomerVO : customers1) {
    for (Customer customerHubVO : customers2) {
        localCustomerVO = fillCustomerDetails(localCustomerVO, customerHubVO);
        customerVoList.add(localCustomerVO);
    }
}

private Customer fillCustomerDetails(Customer localCustomerVO, Customer customerHuvVO) {
        localCustomerVO.setFirstName(StringUtils.isEmpty(customerHuvVO.getFirstName()) ? localCustomerVO.getFirstName() : customerHuvVO.getFirstName());
        localCustomerVO.setLastName(StringUtils.isEmpty(customerHuvVO.getLastName()) ? localCustomerVO.getLastName() : customerHuvVO.getLastName());
        localCustomerVO.setEmailId(StringUtils.isEmpty(customerHuvVO.getEmailId()) ? localCustomerVO.getEmailId() : customerHuvVO.getEmailId());
        localCustomerVO.setBirthDate(StringUtils.isEmpty(customerHuvVO.getBirthDate()) ? localCustomerVO.getBirthDate() : customerHuvVO.getBirthDate());
        localCustomerVO.setFormatedDob(customerHuvVO.getBirthDate() != null ? DateUtils.getDateddMMYYYY(customerHuvVO.getBirthDate()) : (localCustomerVO.getBirthDate() != null ? DateUtils.getDateddMMYYYY(localCustomerVO.getBirthDate()) : ""));

        localCustomerVO.setAdd1(StringUtils.isEmpty(customerHuvVO.getAdd1()) ? localCustomerVO.getAdd1() : customerHuvVO.getAdd1());
        localCustomerVO.setAdd2(StringUtils.isEmpty(customerHuvVO.getAdd2()) ? localCustomerVO.getAdd2() : customerHuvVO.getAdd2());
        localCustomerVO.setAdd3(StringUtils.isEmpty(customerHuvVO.getAdd3()) ? localCustomerVO.getAdd3() : customerHuvVO.getAdd3());
        localCustomerVO.setCity(StringUtils.isEmpty(customerHuvVO.getCity()) ? localCustomerVO.getCity() : customerHuvVO.getCity());
        localCustomerVO.setState(StringUtils.isEmpty(customerHuvVO.getState()) ? localCustomerVO.getState() : customerHuvVO.getState());
        localCustomerVO.setPincode(StringUtils.isEmpty(customerHuvVO.getPincode()) ? localCustomerVO.getPincode() : customerHuvVO.getPincode());

        return localCustomerVO;
}

以上代码工作正常。但我正在寻找其他最佳方式。

2 个答案:

答案 0 :(得分:0)

如果确实只想将customers1的记录#0与customers2的记录#0相匹配,请记录customers1的记录#1 {1}}的记录#1,等等,做这样的事情:

customers2

我要求你考虑这可能不是你想要的那种可能性,但如果是,那基本上就是你的做法。

答案 1 :(得分:0)

检查customerHuvVO属性是否为空,如果是,则将localCustomerVO属性的值设置为不必要的相同值。

所以不要写:

localCustomerVO.setFirstName(StringUtils.isEmpty(customerHuvVO.getFirstName()) ? localCustomerVO.getFirstName() : customerHuvVO.getFirstName());

你可以写:

if (!StringUtils.isEmpty(customerHuvVO.getFirstName())) localCustomerVO.setFirstName(customerHuvVO.getFirstName());

因此消除了不必要的更新。

你也应该检查你的逻辑,因为@Eran指出。