Sunday, 16 March 2014

List Comparision in JAVA

Following details gives you the explanation of how to compare two lists in java and find out  which record are updated, modified and added in that lists.

For this i have created one bean and added the beans to two lists and compare these two lists.

JAVA Bean Class:

public class SampleBean {

    private String id;
    private String name;
    private String age;
    private String salary;
    private String role;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public boolean equal(Object thisObject) {

        SampleBean sampleBean = (SampleBean) thisObject;
        if (sampleBean.getId().equals(this.getId())) {
            return true;
        }
        return false;
    }

    public int hashcode() {
        return 0;

    }

    public boolean compare(SampleBean sampleBean) {

        if (sampleBean == null) {
            return false;
        }
        if (this.getId().equals(sampleBean.getId())
                && this.getAge().equals(sampleBean.getAge())
                && this.getName().equals(sampleBean.getName())
                && this.getRole().equals(sampleBean.getRole())
                && this.getSalary().equals(sampleBean.getSalary())) {
            return true;

        }
        return false;

    }
}
************************************************

Main Class:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SampleListComparision {

    /**
     * @param args
     */
    public static void main(String[] args) {

        SampleBean sampleBean1 = new SampleBean();
        SampleBean sampleBean2 = new SampleBean();
        SampleBean sampleBean3 = new SampleBean();
        SampleBean sampleBean4 = new SampleBean();
        SampleBean sampleBean5 = new SampleBean();
        SampleBean sampleBean6 = new SampleBean();

        sampleBean1.setId("1");
        sampleBean1.setName("java");
        sampleBean1.setAge("19");
        sampleBean1.setRole("language");
        sampleBean1.setSalary("1k");

        sampleBean2.setId("2");
        sampleBean2.setName("servelts");
        sampleBean2.setAge("19");
        sampleBean2.setRole("web");
        sampleBean2.setSalary("2k");

        sampleBean3.setId("3");
        sampleBean3.setName("jsp");
        sampleBean3.setAge("19");
        sampleBean3.setRole("webpage");
        sampleBean3.setSalary("3k");

        sampleBean4.setId("4");
        sampleBean4.setName("spring");
        sampleBean4.setAge("19");
        sampleBean4.setRole("framework");
        sampleBean4.setSalary("4k");

        sampleBean5.setId("1");
        sampleBean5.setName("java");
        sampleBean5.setAge("19");
        sampleBean5.setRole("ProgrmLanguage");
        sampleBean5.setSalary("500k");

        sampleBean6.setId("6");
        sampleBean6.setName("html");
        sampleBean6.setAge("19");
        sampleBean6.setRole("design");
        sampleBean6.setSalary("6k");

        List<SampleBean> old_List = new ArrayList<SampleBean>();
        List<SampleBean> new_List = new ArrayList<SampleBean>();
        old_List.add(sampleBean1);
        old_List.add(sampleBean2);
        old_List.add(sampleBean3);
      
        new_List.add(sampleBean4);
        new_List.add(sampleBean5);
        new_List.add(sampleBean6);
        SampleListComparision comparision = new SampleListComparision();
        comparision.compareLists(new_List, old_List);
    }
  
    /**
     * Here we need to find which records are updated, added and deleted
     * based on old and new lists.
     */
    /**
     * If one record is there in old_list and not there in new_list, then
     * this means it is deleted. For this we need to consider property id of
     * bean.i.e, sampleBean2, sampleBean3 are deleted beans.
     */
    /**
     * If one record is there in new_list and not there in old_list, then
     * this means it is added. For this we need to consider property id of
     * bean.i.e, sampleBean4, sampleBean6 are added beans.
     */
    /**
     * If record is there in both the lists, then it may be either updated
     * if any one of other property of bean changed or same if none of
     * property is changed.i.e, sampleBean5 is updated bean of sampleBean1.
     */
    /**
     * Following method will do the above specified scenario.
     */
    public void compareLists(List<SampleBean> newList , List<SampleBean> oldList){
      
        List<SampleBean> addedList = new ArrayList<SampleBean>(newList);
        List<SampleBean> deletedList = new ArrayList<SampleBean>(oldList);
        List<SampleBean> modifiedList = new ArrayList<SampleBean>();
      
        try {
            Iterator<SampleBean> iterator = newList.iterator();
            while (iterator.hasNext()) {
                SampleBean newSampleBean = (SampleBean) iterator.next();
              
                for (SampleBean oldSampleBean : oldList) {
                  
                    if(oldSampleBean.getId().equals(newSampleBean.getId())){
                        addedList.remove(newSampleBean);
                        deletedList.remove(oldSampleBean);
                        if (oldSampleBean.compare(newSampleBean)) {
                          
                        }else{
                            modifiedList.add(newSampleBean);
                        }
                    }
                  
                }
              
            }
            for (SampleBean sampleBean : modifiedList) {
                System.out.println("modified beans are: ID is" + sampleBean.getId());
            }
            for (SampleBean sampleBean : addedList) {
                System.out.println("added beans are: ID is" + sampleBean.getId());
            }
            for (SampleBean sampleBean : deletedList) {
                System.out.println("deleted beans are: ID is" + sampleBean.getId());
            }
        } catch (Exception e) {
            System.out.println(e);
        }
      
    }

}