/**
  * Removes from this collection all of its elements that are contained in the specified collection
  * (optional operation).
  *
  * <p>This implementation iterates over this collection, checking each element returned by the
  * iterator in turn to see if it's contained in the specified collection. If it's so contained,
  * it's removed from this collection with the iterator's <tt>remove</tt> method.
  *
  * <p>Note that this implementation will throw an <tt>UnsupportedOperationException</tt> if the
  * iterator returned by the <tt>iterator</tt> method does not implement the <tt>remove</tt> method
  * and this collection contains one or more elements in common with the specified collection.
  *
  * @param c elements to be removed from this collection.
  * @return <tt>true</tt> if this collection changed as a result of the call.
  * @throws UnsupportedOperationException if the <tt>removeAll</tt> method is not supported by this
  *     collection.
  * @throws NullPointerException if the specified collection is null.
  * @see #remove(Object)
  * @see #contains(Object)
  */
 public boolean removeAll(Collection<?> c) {
   boolean modified = false;
   Iterator<?> i = c.iterator();
   while (i.hasNext()) {
     modified |= remove(i.next());
   }
   return modified;
 }
 /**
  * Adds all of the elements in the specified collection to this collection (optional operation).
  * The behavior of this operation is undefined if the specified collection is modified while the
  * operation is in progress. (This implies that the behavior of this call is undefined if the
  * specified collection is this collection, and this collection is nonempty.)
  *
  * <p>This implementation iterates over the specified collection, and adds each object returned by
  * the iterator to this collection, in turn.
  *
  * <p>Note that this implementation will throw an <tt>UnsupportedOperationException</tt> unless
  * <tt>add</tt> is overridden (assuming the specified collection is non-empty).
  *
  * @param c collection whose elements are to be added to this collection.
  * @return <tt>true</tt> if this collection changed as a result of the call.
  * @throws UnsupportedOperationException if this collection does not support the <tt>addAll</tt>
  *     method.
  * @throws NullPointerException if the specified collection is null.
  * @see #add(Object)
  */
 public boolean addAll(Collection<? extends T> c) {
   boolean modified = false;
   Iterator<? extends T> e = c.iterator();
   while (e.hasNext()) {
     if (add(e.next())) modified = true;
   }
   return modified;
 }
 /**
  * Returns <tt>true</tt> if this collection contains the specified element. More formally, returns
  * <tt>true</tt> if and only if this collection contains at least one element <tt>e</tt> such that
  * <tt>(o==null ? e==null : o.equals(e))</tt>.
  *
  * <p>This implementation iterates over the elements in the collection, checking each element in
  * turn for equality with the specified element.
  *
  * @param o object to be checked for containment in this collection.
  * @return <tt>true</tt> if this collection contains the specified element.
  */
 public boolean contains(Object o) {
   Iterator<T> e = iterator();
   if (o == null) {
     while (e.hasNext()) if (e.next() == null) return true;
   } else {
     while (e.hasNext()) if (o.equals(e.next())) return true;
   }
   return false;
 }
 /**
  * Retains only the elements in this collection that are contained in the specified collection
  * (optional operation). In other words, removes from this collection all of its elements that are
  * not contained in the specified collection.
  *
  * <p>This implementation iterates over this collection, checking each element returned by the
  * iterator in turn to see if it's contained in the specified collection. If it's not so
  * contained, it's removed from this collection with the iterator's <tt>remove</tt> method.
  *
  * <p>Note that this implementation will throw an <tt>UnsupportedOperationException</tt> if the
  * iterator returned by the <tt>iterator</tt> method does not implement the <tt>remove</tt> method
  * and this collection contains one or more elements not present in the specified collection.
  *
  * @param c elements to be retained in this collection.
  * @return <tt>true</tt> if this collection changed as a result of the call.
  * @throws UnsupportedOperationException if the <tt>retainAll</tt> method is not supported by this
  *     Collection.
  * @throws NullPointerException if the specified collection is null.
  * @see #remove(Object)
  * @see #contains(Object)
  */
 public boolean retainAll(Collection<?> c) {
   ArrayList<T> toBeRemoved = new ArrayList<T>();
   Iterator<T> i = iterator();
   while (i.hasNext()) {
     T o = i.next();
     if (!c.contains(o)) {
       toBeRemoved.add(o);
     }
   }
   int n = toBeRemoved.size();
   for (int j = 0; j < n; j++) {
     remove(toBeRemoved.get(j));
   }
   return n != 0;
 }
 /**
  * Removes a single instance of the specified element from this collection, if it is present
  * (optional operation). More formally, removes an element <tt>e</tt> such that <tt>(o==null ?
  * e==null : o.equals(e))</tt>, if the collection contains one or more such elements. Returns
  * <tt>true</tt> if the collection contained the specified element (or equivalently, if the
  * collection changed as a result of the call).
  *
  * <p>This implementation iterates over the collection looking for the specified element. If it
  * finds the element, it removes the element from the collection using the iterator's remove
  * method.
  *
  * <p>Note that this implementation throws an <tt>UnsupportedOperationException</tt> if the
  * iterator returned by this collection's iterator method does not implement the <tt>remove</tt>
  * method and this collection contains the specified object.
  *
  * @param o element to be removed from this collection, if present.
  * @return <tt>true</tt> if the collection contained the specified element.
  * @throws UnsupportedOperationException if the <tt>remove</tt> method is not supported by this
  *     collection.
  */
 public boolean remove(Object o) {
   Iterator<T> e = iterator();
   if (o == null) {
     while (e.hasNext()) {
       if (e.next() == null) {
         e.remove();
         return true;
       }
     }
   } else {
     while (e.hasNext()) {
       if (o.equals(e.next())) {
         e.remove();
         return true;
       }
     }
   }
   return false;
 }
 /**
  * Returns <tt>true</tt> if this collection contains all of the elements in the specified
  * collection.
  *
  * <p>This implementation iterates over the specified collection, checking each element returned
  * by the iterator in turn to see if it's contained in this collection. If all elements are so
  * contained <tt>true</tt> is returned, otherwise <tt>false</tt>.
  *
  * @param c collection to be checked for containment in this collection.
  * @return <tt>true</tt> if this collection contains all of the elements in the specified
  *     collection.
  * @throws NullPointerException if the specified collection is null.
  * @see #contains(Object)
  */
 public boolean containsAll(Collection<?> c) {
   Iterator<?> e = c.iterator();
   while (e.hasNext()) if (!contains(e.next())) return false;
   return true;
 }