Home > front end >  remove data from one list with another list data function not working properly
remove data from one list with another list data function not working properly

Time:10-03

public static void getIndividualsList() {
        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);

        individualList.removeAll(individualList1);
        
        for (Employee employee : individualList) {
            System.out.println(employee.getDateOfBirth());
        }
    }

Why removeAll is not working properly?

Applied removeAll but still getting data. Can someone please let me know why its not working?

CodePudding user response:

Based on your comment: "In the employee class there is restriction. So its not allowing to modify like adding any other methods or comparator. Thats the problem. Thats why trying to find another solution for that."

If you have any unique field, maybe you can use that field to iterate over your first list to remove the required items.

import java.util.*;

public class MyClass {
    
    public static void main(String args[]) {
      List<Employee> listA = new ArrayList<>();
      listA.add(new Employee("gokul", "1", "[email protected]"));
      listA.add(new Employee("user", "2", "[email protected]"));
      
      List<Employee> listB = new ArrayList<>();
      listB.add(new Employee("user", "2", "[email protected]"));
      
      for (Employee eB : listB) {
          for (Employee eA : listA) {
              if (eA.id.equals(eB.id)) {
                  listA.remove(eA);
                  break;
              }
          }
      }
      
      for (Employee e : listA) {
          System.out.println(e.name);
      }
    }
    
    static class Employee {
        String name;
        String id;
        String email;
        
        public Employee(String name, String id, String email) {
            this.name = name;
            this.id = id;
            this.email = email;
        }
    }
}

CodePudding user response:

Both remove and removeAll relies on equal (and hashCode!) implementation. Most probably you didn't implement one of this methods.

Without custom implementation of equal an object equals only himself. Two variables must reference exactly the same object to be equal. Custom implementation looks like this:

class Employee {
    ....
    public boolean equals(Object obj) {
        if (obj instanceof Employee) {
            Employee oo = (Employee)obj;
            return Objects.equal(fullName, oo.fullName) && Objects.equal(dateOfBirth, oo.dateOfBirth)
        } else {
            return false;
        }
    }

    public int hashCode() {
        return Objects.hashCode(fullName, dateOfBirth);
    }
}

I took fullName and dateOfBirth to ensure equality. You must manually add all the fields you think important for two objects to be equal (spoiler - it is not trivial).


If you cannot modify the object Employee there are to ways:

  • do it manually as described by "Gokul Nath KP"
  • take some other collection structure instead of ArrayList. For example TreeSet uses an optionally provided custom Comparator instance instead of equal-Method.
  •  Tags:  
  • java
  • Related