Compare two arrays of content and store unsurpassed content in another arraylist

I want to compare two contents of arraylist.

I store objects in them that way.

for Arraylist 1:

Employee e1=new Employee(); e1.setID("1"); e1.setID("2"); ArrayList<Employee>list1 = new ArrayList<Employee>(); if(e1!=null){ list1.add(e1); } 

for Arraylist 2:

 Employee e2=new Employee(); e2.setID("1"); e2.setID("2"); e2.setID("4"); ArrayList<Employee>list2 = new ArrayList<Employee>(); if(e2!=null){ list2.add(e2); } 

Now I am trying to compare the above arraylist contents in this way

 ArrayList<Employee>unmatchedList = new ArrayList<Employee>(); for (Employee l1 : list1){ if(!list2.contains(l1.getID())){ System.out.println("list2 does not contains this ID"+l1.getID()); Employee e3=new Employee(); e3.setID(l1.getID()); if(unmatchedList==null){ unmatchedList=new ArrayList<Employee>(); unmatchedList.add(e3); } if(unmatchedList!=null){ unmatchedList.add(e3); } } } 

But I do not get the correct unmatchedList content only as "4". I get unmatchedList as "1" and "2", which is wrong. So how can I get unsurpassed content only in "unmatchedList"

+4
source share
5 answers

If your Employee class is defined as follows:

 public class Employee { private int id; public Employee(int id){ this.id = id; } public int getId() { return id; } @Override public String toString() { return "Id : " + this.id; } @Override public boolean equals(Object obj) { return (obj instanceof Employee) && this.id == ((Employee)obj).getId(); } 

in the main method, you can get the unmatch contents as follows:

  public static void main( String[] args ) { List<Employee> l1 = new ArrayList<Employee>(); l1.add(new Employee(1)); l1.add(new Employee(2)); l1.add(new Employee(3)); l1.add(new Employee(4)); l1.add(new Employee(5)); List<Employee> l2 = new ArrayList<Employee>(); l2.add(new Employee(4)); l2.add(new Employee(5)); l1.removeAll(l2); System.out.println(l1); } 

This will print: [Id : 1, Id : 2, Id : 3]

Note that you must override the equals method for this to work.

+5
source

The problem is this: -

 if(!list2.contains(l1.getID())){ 

Your list2 is an ArrayList<Employee> , and you check the localization of l1.getId() . So your if condition will always be true.


You should rather override the equals and hashCode method in your Employee class and just use: -

 if(!list2.contains(l1)) 

to check if list2 contains employee l1 .


And why do you set your id 3 times back before adding it to the list. It will not add all three identifiers, but only one Employee with the last value set for id. You must fix: -

 e2.setID("1"); e2.setID("2"); e2.setID("4"); list.add(e2); // Will only add one Employee with id = 4 
+1
source

Add the equals method to your employees, which compares them by their identifier, creates a copy of list2 and calls removeAll :

 List<Employee> list1 = ...; List<Employee> list2 = ...; List<Employee> unmatchedList = new ArrayList<Employee>(list2); unmatchedList.removeAll(list1); 

See @Dimitri answer for a complete example.

+1
source

I think you should use Set for your scenario. Perhaps these examples will help you.

Employee Class

 package com.yourcomp; /** * <br> * <div style="width:600px;text-align:justify;"> * * TODO: Class comment. * * </div> * <br> * */ public class Employee { private int id; /** * Constructor for Employee. <tt></tt> */ public Employee() { this(-1); } /** * Constructor for Employee. <tt></tt> */ public Employee(int id) { this.id = id; } /** * Gets the id. * * @return <tt> the id.</tt> */ public int getId() { return id; } /** * Sets the id. * * @param id <tt> the id to set.</tt> */ public void setId(int id) { this.id = id; } @Override public boolean equals(Object obj) { return this.id == ((Employee)obj).id; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Employee [id=" + id + "]"; } /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { // TODO Auto-generated method stub return new Integer(id).hashCode(); } } 

Class TestEmployee :

 package com.yourcomp; import java.util.HashSet; import java.util.Set; /** * <br> * <div style="width:600px;text-align:justify;"> * * TODO: Class comment. * * </div> * <br> * */ public class TestEmployee { public static void main(String[] args) { // creating the first set with employees having ID 1 to 5 Set<Employee> set1 = new HashSet<Employee>(); for(int i=1;i<5;i++) set1.add(new Employee(i)); System.out.println(set1); // printing it // creating the first set with employees having ID 3 to 8. // note that now you have two employees with same ID, 3 & 4 Set<Employee> set2 = new HashSet<Employee>(); for(int i=3;i<8;i++) set2.add(new Employee(i)); System.out.println(set2);// printing the second set // creates a final set to contain all elements from above two sets without duplicates Set<Employee> finalSet = new HashSet<Employee>(); for(Employee employee:set1) finalSet.add(employee); // adds first set content for(Employee employee:set2) finalSet.add(employee); // adds second set content. If any duplicates found, it will be overwritten System.out.println(finalSet); // prints the final set } } 

and conclusion

 [Employee [id=1], Employee [id=2], Employee [id=3], Employee [id=4]] [Employee [id=3], Employee [id=4], Employee [id=5], Employee [id=6], Employee [id=7]] [Employee [id=1], Employee [id=2], Employee [id=3], Employee [id=4], Employee [id=5], Employee [id=6], Employee [id=7]] 
0
source

I would also recommend Set to make a difference, and if you can use Sets, I would use the Guava-Sets difference method: http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ Sets.html # difference% 28java.util.Set,% 20java.util.Set% 29

0
source

Source: https://habr.com/ru/post/1447689/


All Articles