简体   繁体   中英

Compare pojo Values from given 2 Strings

I have a pojo class:

class Employee{
    private int empCode; 
    private String name;
//getters and setters and Constructor
}

I have another class which has all the Employee Object instantiated using the parameter constructor. and all the Employee pojo objects are added in the Arraylist.

class EmployeeData{
    Employee employee = new Employee(111,"XXX");
    Employee employee1 = new Employee(222, "YYY");
    Employee employee2 = new Employee(444, "BBB");
    List<Employee> listData  = new ArrayList<Employee>();
    listData.add(employee);
    listData.add(employee1);
    listData.add(employee2);

    public List<Employee> getList(List<Employee> list){
        return list;
    }
}

lastly i have a Main class which has another list and a merge method.

class EmployeeTest{
    Employee employee = new Employee(111,"NNN");
    Employee employee1 = new Employee(222, "YYY");
    Employee employee2 = new Employee(333, "KKK");
    List<Employee> list  = new ArrayList<Employee>();
    list.add(employee);
    list.add(employee1);
    list.add(employee2);  
    public List<Employee> merge(List<Employee> list){
    //code goes here
    return listData;
}
  1. I need to compare the id's from both the list and if the id's matches but the names are different I will overwrite the listData's values with the list's values.

  2. if the ids doesn't matches and like 333 doesn't exist in listData. so we will add the id and values, and Lastly

  3. if some id from listData doesn't matches with the list we will remove it, eg we will remove 444 from listData, and finally we will return the listData.

I tried to do something like this but I am not getting the proper output. Please help.

public List<Employee> merge(List<Employee> list){
    if(list==null){
        return null;
    }
    List<Employee> list1 = employeeDao.getList();
    for(int i =0;i<list1.size();i++){
        for(int j=0; j<list1.size();j++){
            if(list.get(i).getEmpCode().equals(list2.get(j).getEmpCode())){
                list1.set(j, list.get(i));
                j++;
            }else{
                j++;
            }
        }
    }
    return list;
}

I am not able to think about the last 2 logics. Please help

Check whether you are getting correct answer or not. It is not good approach but still.

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


class Employee {
    private int empCode;
    private String name;
    public Employee(int empCode, String name) {
        super();
        this.empCode = empCode;
        this.name = name;
    }
    public int getEmpCode() {
        return empCode;
    }
    public void setEmpCode(int empCode) {
        this.empCode = empCode;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Employee [empCode=" + empCode + ", name=" + name + "]";
    }

}



public class Practice {

    public static void main(String[] args) {

        Employee employee = new Employee(111,"XXX");
        Employee employee1 = new Employee(222, "YYY");
        Employee employee2 = new Employee(444, "BBB");


        Employee employee3 = new Employee(111,"NNN");
        Employee employee4 = new Employee(222, "YYY");
        Employee employee5 = new Employee(333, "KKK");

        List<Employee> employeeDataList = new ArrayList<Employee>();
        employeeDataList.add(employee);
        employeeDataList.add(employee1);
        employeeDataList.add(employee2);


        List<Employee> employeeTestList = new ArrayList<Employee>();
        employeeTestList.add(employee3);
        employeeTestList.add(employee4);
        employeeTestList.add(employee5);

        merge(employeeDataList, employeeTestList);

    }

    public static List<Employee> merge(List<Employee> employeeDataList, List<Employee> employeeTestList) {
        List<Employee> list = new ArrayList<Employee>();

        for(Employee emp : employeeTestList) {
            int lengthOfEmployeeDataList = employeeDataList.size();
            int count = 0;
            for(Employee empData : employeeDataList){
                if(emp.getEmpCode() == empData.getEmpCode()) {
                    //EMPLOYEE CODE MATCHES
                    if(!emp.getName().equals(empData.getName())) {
                        //WHEN NAME ARE NOT EQUAL
                        empData.setName(emp.getName()); //NAME IN TEST LIST IS ASSIGNED TO DATALIST
                        list.add(empData);
                        break;
                    }
                    else {
                        list.add(emp);
                        break;
                    }
                }
                else if(!(emp.getEmpCode() == empData.getEmpCode()) && count < lengthOfEmployeeDataList){
                    //ID DOESNT MATCHES
                    count++;
                }
                if(count == lengthOfEmployeeDataList){
                    //ALL THE EMPLOYEE Objects has been Checked and None Matching
                    list.add(emp);
                }

            }
        }
        for(Employee emp : employeeDataList) {
            if(!list.contains(emp)) {
                employeeDataList.remove(emp);
            }
        }
        System.out.println(list);
        return list;
    }


}

If List is not the only solution, I would suggest to use Class HashMap and use as keys the ids. Then you can use the HashMap api which is more helpful for such actions eg putIfAbsent() https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM