title: Best way to implement retainAll() method
body: <p>I've this custom class named <code>MyAbstractList</code> which implements <code>MyList</code> interface. Here's the code:</p>
  
  <pre><code>public abstract class MyAbstractList&lt;E&gt; implements MyList&lt;E&gt; {
      protected int size = 0; // The size of the list
  
      protected MyAbstractList() {
      }
  
      protected MyAbstractList(E[] objects) {
          for (int i = 0; i &lt; objects.length; i++)
              add(objects[i]);
      }
  
      public void add(E e) {
          add(size, e);
      }
  
      public boolean isEmpty() {
          return size == 0;
      }
  
      public int size() {
          return size;
      }
  
      public boolean addAll(MyList&lt;E&gt; otherList) {
          for (E e : otherList) {
              add(e);
          }
          if (otherList.size() &gt; 0)
              return true;
          return false;
      }
  
      public boolean removeAll(MyList&lt;E&gt; otherList) {
          boolean removed = false;
          for (E e : otherList) {
              if (remove(e) &amp;&amp; !removed)
                  removed = true;
          }
          return removed;
      }
  
      public boolean remove(E e) {
          if (indexOf(e) &gt;= 0) {
              remove(indexOf(e));
              return true;
          } else
              return false;
      }
  
      /** Retains the elements in this list that are also in otherList
       *  Returns true if this list changed as a result of the call      */
      public boolean retainAll(MyList&lt;E&gt; otherList) {
  
      }
  }
  </code></pre>
  
  <p>How to implement the <code>retainAll()</code> method? </p>
  
  <p><code>MyList</code> interface:</p>
  
  <pre><code>public interface MyList&lt;E&gt; extends java.lang.Iterable&lt;E&gt; {
    /** Add a new element at the end of this list */
    public void add(E e);
  
    /** Add a new element at the specified index in this list */
    public void add(int index, E e);
  
    /** Clear the list */
    public void clear();
  
    /** Return true if this list contains the element */
    public boolean contains(E e);
  
    /** Return the element from this list at the specified index */
    public E get(int index);
  
    /** Return the index of the first matching element in this list.
     *  Return -1 if no match. */
    public int indexOf(E e);
  
    /** Return true if this list contains no elements */
    public boolean isEmpty();
  
    /** Return the index of the last matching element in this list
     *  Return -1 if no match. */
    public int lastIndexOf(E e);
  
    /** Remove the first occurrence of the element o from this list.
     *  Shift any subsequent elements to the left.
     *  Return true if the element is removed. */
    public boolean remove(E e);
  
    /** Remove the element at the specified position in this list
     *  Shift any subsequent elements to the left.
     *  Return the element that was removed from the list. */
    public E remove(int index);
  
    /** Replace the element at the specified position in this list
     *  with the specified element and returns the new set. */
    public Object set(int index, E e);
  
    /** Return the number of elements in this list */
    public int size();
  
    /** Adds the elements in otherList to this list.
    * Returns true if this list changed as a result of the call */
    public boolean addAll(MyList&lt;E&gt; otherList);
  
    /** Removes all the elements in otherList from this list
    * Returns true if this list changed as a result of the call */
    public boolean removeAll(MyList&lt;E&gt; otherList);
  
    /** Retains the elements in this list that are also in otherList
    * Returns true if this list changed as a result of the call */
    public boolean retainAll(MyList&lt;E&gt; otherList);
  
    /** Return an iterator for the list */
    public java.util.Iterator&lt;E&gt; iterator();
  }
  </code></pre>