Compare two List objects contains same objects with duplicate entries, but we need to show the unique records to the user. To achieve the above the following is the small snippet.
CustomerVO.java
public class CustomerVO implements Comparable{
private Integer id;
private String name;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
return id.compareTo(((CustomerVO)obj).getId());
}
}
Test.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test {
/**
* Method generate the master records for the customer information
* @return an Instance of <tt>ArrayList</tt> object
*/
private List<CustomerVO> getFirstArrayList() {
CustomerVO customer = null;
System.out.println("Creating Customer First List");
List<CustomerVO> customerFirstList = new ArrayList<CustomerVO>();
for (int i = 0; i < 100; i++) {
customer = new CustomerVO();
customer.setId(i + 1);
customer.setName("Name " + (i + 1));
customerFirstList.add(customer);
}
/**
* Sorting resultant list
*/
if(null != customerFirstList){
Collections.sort(customerFirstList);
}
return customerFirstList;
}
/**
* Method generate the duplicate records for the customer information
* @return an Instance of <tt>ArrayList</tt> object
*/
private List<CustomerVO> getSecondArrayList() {
System.out.println("Creating Customer First List");
CustomerVO customer = null;
List<CustomerVO> customerSeconbdList = new ArrayList<CustomerVO>();
for (int i = 1; i <= 50; i++) {
customer = new CustomerVO();
customer.setId(2*i);
customer.setName("Name " + (2 * i));
customerSeconbdList.add(customer);
}
/**
* Sorting resultant list
*/
if(null != customerSeconbdList){
Collections.sort(customerSeconbdList);
}
return customerSeconbdList;
}
/**
* Method to clean the lists and get unique records as list
* @param firstList
* Master/duplicate records for customer information
* @param secondList
* Master/duplicate records for customer information
* @return uniqeList an instance of <tt>ArrayList</tt>
* contains the unique customer records
*/
private List<CustomerVO> getUniqueCustomerList(List<CustomerVO> firstList,
List<CustomerVO> secondList) {
List<CustomerVO> biggerList = null;
List<CustomerVO> smallestList = null;
List<CustomerVO> uniqeList = null;
if(firstList != null && secondList != null ){
/**
* INITIALIZE THE BIGGEST AND SMALLEST LIST
*/
if( firstList.size() < secondList.size()){
biggerList = secondList;
smallestList = firstList;
}else{
biggerList = firstList;
smallestList = secondList;
}
/**
* PROCESSING THE LIST TO ELIMINATE THE DUPLICATE RECORDS FROM THE GIVE LIST
*/
if(biggerList.size() >0 && smallestList.size() > 0){
uniqeList = new ArrayList<CustomerVO>();
for(int i = 0 ; i< biggerList.size() ; ){
for(int j=0 ;j < smallestList.size(); ){
if(smallestList.get(j).compareTo(biggerList.get(i)) <= 0){
/** BOTH OBJECTS ARE THE SAME OR VALUE AT J INDEX IS LESS THAN VALUE AT I INDEX
* ADD OBJECT TO UNIQUE LIST
* AND INCREMENT I,J INDEX VALUES BY 1**/
uniqeList.add(smallestList.get(j));
System.out.println("equal");
i++;
j++;
}else if(smallestList.get(j).compareTo(biggerList.get(i)) < 0){
/** BOTH OBJECTS ARE NOT SAME
* VALUE AT J INDEX IS LESS THAN VALUE AT I INDEX
* ADD THE SMALL VALUE TO THE UNIQUE LIST
* INCREMENT J INDEX VALUE BY 1 **/
uniqeList.add(biggerList.get(i));
System.out.println("LESSTAHN");
j++;
} else if(smallestList.get(j).compareTo(biggerList.get(i)) > 0){
/** BOTH OBJECTS ARE NOT SAME
* VALUE AT J INDEX IS GRATTER THAN VALUE AT I INDEX
* ADD THE SMALL VALUE TO THE UNIQUE LIST
* INCREMENT I INDEX VALUE BY 1 **/
uniqeList.add(biggerList.get(i));
System.out.println("GREATERTAHN");
i++;
} else{
}
}
}
}
for(CustomerVO customer : uniqeList){
System.out.println("ID :"+customer.getId());
}
}
return null;
}
public static void main(String[] args) {
System.out.println("Creating Customer Second List");
Test t = new Test();
t.getUniqueCustomerList(t.getFirstArrayList(), t.getSecondArrayList());
}
}
No comments:
Post a Comment