title: How to use Collections methods(removeAll() and retainAll()) for two objects
body: <p>I expected to result below but actually not. Even though it worked when I tried to use String instead of Item Object.
  I would like to know why the reasons about that and how to code in order to get expected result.Thanks.</p>
  
  <pre><code>EXPECTED
  ------------------------------
  removed object are:
  2
  same object are:
  1
  3
  add object are:
  4
  ------------------------------
  </code></pre>
  
  <hr>
  
  <pre><code>ACTUAL
  ------------------------------
  removed object are:
  1
  2
  3
  same object are:
  add object are:
  1
  3
  4
  ------------------------------
  </code></pre>
  
  <hr>
  
  <pre><code>package com.javastudy;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class CollectionCompareToObjects {
  
   public static void main(String[] args) {
  
    List&lt;Item&gt; before = new ArrayList&lt;Item&gt;();
    List&lt;Item&gt; after = new ArrayList&lt;Item&gt;();
  
    before.add(new Item(1L));
    before.add(new Item(2L)); // delete
    before.add(new Item(3L));
  
    after.add(new Item(1L));
    after.add(new Item(3L));
    after.add(new Item(4L)); // added
  
    List&lt;Item&gt; removed = new ArrayList&lt;Item&gt;(before);
    removed.removeAll(after);
  
    System.out.println("removed objects are:");
    for(Item item : removed){
     System.out.println(item.getId());
    }
  
    List&lt;Item&gt; same = new ArrayList&lt;Item&gt;(before);
    same.retainAll(after);
  
    System.out.println("same objects are:");
    for(Item item : same){
     System.out.println(item.getId());
    }
  
    List&lt;Item&gt; added = new ArrayList&lt;Item&gt;(after);
    added.removeAll(before);
  
    System.out.println("add objects are:");
    for(Item item : added){
     System.out.println(item.getId());
    }
  
   }
  
  }
  
  package com.javastudy;
  
  public class Item {
  
   Long id;
  
   public Item(Long id) {
    this.id = id;
   }
  
   public Long getId() {
    return id;
   }
  
   public void setId(Long id) {
    this.id = id;
   }
  
  }
  </code></pre>