I have 2 list of same object. 2 list may contains same data or may be different. From the below program trying to find if any mismatch data is exist or not. Please find below program
public static void getMismathcEmployee() {
List<Employee> individualList = new ArrayList<>();
Employee individualDtls = new Employee();
individualDtls.setDependentId(0);
individualDtls.setDateOfBirth("06/06/1998");
individualDtls.setEeId(1L);
individualDtls.setFullName("MICHAEL K HERNANDEZ");
individualDtls.setCovered(false);
individualDtls.setDependentType("Self");
individualList.add(individualDtls);
List<Employee> individualList1 = new ArrayList<>();
Employee individualDtls1 = new Employee();
individualDtls1.setDependentId(0);
individualDtls1.setDateOfBirth("06/06/1998");
individualDtls1.setEeId(1L);
individualDtls1.setFullName("MICHAEL K HERNANDEZ");
individualDtls1.setCovered(false);
individualDtls1.setDependentType("Self");
individualList1.add(individualDtls1);
boolean mismatchFound = false;
for (Employee employee : individualList) {
long depId = employee.getDependentId();
String dob = employee.getDateOfBirth();
long eeId = employee.getEeId();
String name = employee.getFullName();
String type = employee.getDependentType();
for (Employee employee2 : individualList1) {
long depId1 = employee2.getDependentId();
String dob1 = employee2.getDateOfBirth();
long eeId1 = employee2.getEeId();
String name1 = employee2.getFullName();
String type1 = employee2.getDependentType();
if (!(depId == depId1 && dob1.equals(dob) && eeId == eeId1 && name.equals(name1) && type.equals(type1))) {
mismatchFound = true;
break;
}
}
}
System.out.println("Mismathc found::" mismatchFound);
}
As you can see I have used nested for loop. Is there any other work around for achive the same result?
CodePudding user response:
You can easily do this by following way.
First override equals()
and hashCode()
methods in Employee
class. This is quite straightforward. If you have modern IDEA like Intellij
, you can simply generate these two.
Secondly check whether the two list are equal or not. You can do that following way,
If order of these list also important to be equal, then you can do this.
Boolean isEqual = individualList.equals(individualList1);
or
Boolean mismatchFound = !individualList.equals(individualList1);
If order of these list not important to be equal, then you can do this.
Boolean isEqual = new HashSet<>(individualList).equals(new HashSet<>(individualList1));
Or
Boolean mismatchFound = !(new HashSet<>(individualList).equals(new HashSet<>(individualList1)));
CodePudding user response:
Current implementation seems to return true only if both lists contain equal elements (each element in list1 is equal to each element of list2). If either list contains two non-equal elements, the "mismatch" occurs.
Thus, both lists may be converted into sets which should be compared (no explicit loop is needed) assuming that the methods equals
and hashCode
are overridden properly in Employee
class.
Also, it makes sense to separate creation of the elements and list population from comparison.
An equivalent comparison of the two lists may be implemented as follows:
public static boolean mismatchFound(List<Employee> one, List<Employee> two) {
Set<Employee> set1 = new HashSet<>(one);
Set<Employee> set2 = new HashSet<>(two);
return set1.size() == 1 && set1.equals(set2);
}