//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Problem 5.10 on page 107

  public boolean retainAll(Collection collection)
  { // removes from this bag all of the object that are not in
    // the given collection, thereby reducing this bag to its
    // set-theoretic complement relative to that collection;
    // returns true iff this bag was modified;
    boolean modified=false;
    for (int i=0; i<size; i++)
      if (!collection.contains(objects[i]))
      { remove(objects[i]);
        modified = true;
      }
    return modified;
  }
