title: Multiple NULL addition into a List in Java
body: <p>I have 2 lists and want to copy some element from one to another, i.e. there are old and new employees list I need to union 2 lists and delete the elements that include to the old list but not include in the new one.</p>
  
  <p>I could solve the part of getting the union and intersection by using TreeSet and override the equals and hashcode functions of the Employees class....</p>
  
  <p>Now, I want to exclude the elements that are in the old but not in the new and add them to the "deletedList"....which I got "ConcurrentModificationException"</p>
  
  <p>I tried this instead of "iterator" but the same result: for(Employees e : employeesListDB)</p>
  
  <p>Also I tried "CopyOnWriteArrayList" instead of "ArrayList" but no change!!</p>
  
  <p>but the problem now that at the initialization of the empty list "deletedList" it is filled with multiple null elements before the add function!</p>
  
  <p>Here is the code:</p>
  
  <pre><code>List&lt;Employees&gt; employeesListDB = this.findAll();     
  
  Set&lt;Employees&gt; empSet = new TreeSet&lt;Employees&gt;(new EmployeeComparator());
  empSet.addAll(employeesList);
  
  List&lt;Employees&gt; deletedList = new ArrayList&lt;Employees&gt;();
  Employees e = new Employees();
  
  ListIterator&lt;Employees&gt; itr = employeesListDB.listIterator();    
  for(itr.hasNext()) { 
    e = (Employees)itr.next();
    if(!empSet.contains(e)) {
      deletedList.add(e);
    }               
  }
  </code></pre>
  
  <p>A counter Example:</p>
  
  <p>The oldlist "employeesListDB" the employees list from the database:</p>
  
  <pre><code>[  
      {  
          "email":"mariam.moustafa@x.com"
      },
      {  
          "email":"sara.ahmed@x.com"
      },
      {  
          "email":"ali.hassan@x.com"
      },
      {  
          "email":"hoosen.imam-ally@x.com"
      },
      {  
          "email":"allan.randall@x.com"
      },
      {  
          "email":"nishaan.maharaj@x.com"
      }
  ]
  </code></pre>
  
  <p>The new list to be added:</p>
  
  <pre><code>[  
      {  
          "email":"ali.moustafa@x.com"
      },
      {  
          "email":"sara.ahmed@x.com"
      },
      {  
          "email":"emad.hamed@x.com"
      }  
  </code></pre>
  
  <p>]</p>
  
  <p>The deleted list that I want:</p>
  
  <blockquote>
    <p>[<br>
            {<br>
                "email":"mariam.moustafa@x.com"
            },
            {<br>
                "email":"ali.hassan@x.com"
            },
            {<br>
                "email":"hoosen.imam-ally@x.com"
            },
            {<br>
                "email":"allan.randall@x.com"
            },
            {<br>
                "email":"nishaan.maharaj@x.com"
            }
        ]</p>
  </blockquote>
  
  <p>Sara mail will be updated...</p>
  
  <p>Employee class has two fields {id,email} the new list (the list to be added to the db) is a list of only emails, id field are not recognized yet but the old list has the complete bean fields ...to compare between these 2 list I should override the Comparator to ignore the id field; <a href="https://stackoverflow.com/questions/27926111/finding-duplicates-in-a-list-ignoring-a-field">Finding duplicates in a List ignoring a field</a></p>
  
  <p>JUST I need to know, why when I use set.add operation, it adds the unique emails only! the original size of the list was 36 elements after adding it into  a set it becomes only 16!!</p>
  
  <pre><code> Set&lt;Employees&gt; oldSet = new TreeSet&lt;Employees&gt;(new EmployeeComparator());
          oldSet.addAll(employeesListDB);
  
          Set&lt;Employees&gt; newSet = new TreeSet&lt;Employees&gt;(new EmployeeComparator());
          newSet.addAll(employeesList);
  
  
          Set&lt;Employees&gt; deleted = Sets.difference(oldSet, newSet);
  </code></pre>