139 * and the specified collection, by invoking the <tt>size</tt>
140 * method on each. If this set has fewer elements, then the
141 * implementation iterates over this set, checking each element
142 * returned by the iterator in turn to see if it is contained in
143 * the specified collection. If it is so contained, it is removed
144 * from this set with the iterator's <tt>remove</tt> method. If
145 * the specified collection has fewer elements, then the
146 * implementation iterates over the specified collection, removing
147 * from this set each element returned by the iterator, using this
148 * set's <tt>remove</tt> method.
149 *
150 * <p>Note that this implementation will throw an
151 * <tt>UnsupportedOperationException</tt> if the iterator returned by the
152 * <tt>iterator</tt> method does not implement the <tt>remove</tt> method.
153 *
154 * @param c collection containing elements to be removed from this set
155 * @return <tt>true</tt> if this set changed as a result of the call
156 * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
157 * is not supported by this set
158 * @throws ClassCastException if the class of an element of this set
159 * is incompatible with the specified collection (optional)
160 * @throws NullPointerException if this set contains a null element and the
161 * specified collection does not permit null elements (optional),
162 * or if the specified collection is null
163 * @see #remove(Object)
164 * @see #contains(Object)
165 */
166 public boolean removeAll(Collection<?> c) {
167 boolean modified = false;
168
169 if (size() > c.size()) {
170 for (Iterator<?> i = c.iterator(); i.hasNext(); )
171 modified |= remove(i.next());
172 } else {
173 for (Iterator<?> i = iterator(); i.hasNext(); ) {
174 if (c.contains(i.next())) {
175 i.remove();
176 modified = true;
177 }
178 }
179 }
180 return modified;
181 }
|
139 * and the specified collection, by invoking the <tt>size</tt>
140 * method on each. If this set has fewer elements, then the
141 * implementation iterates over this set, checking each element
142 * returned by the iterator in turn to see if it is contained in
143 * the specified collection. If it is so contained, it is removed
144 * from this set with the iterator's <tt>remove</tt> method. If
145 * the specified collection has fewer elements, then the
146 * implementation iterates over the specified collection, removing
147 * from this set each element returned by the iterator, using this
148 * set's <tt>remove</tt> method.
149 *
150 * <p>Note that this implementation will throw an
151 * <tt>UnsupportedOperationException</tt> if the iterator returned by the
152 * <tt>iterator</tt> method does not implement the <tt>remove</tt> method.
153 *
154 * @param c collection containing elements to be removed from this set
155 * @return <tt>true</tt> if this set changed as a result of the call
156 * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
157 * is not supported by this set
158 * @throws ClassCastException if the class of an element of this set
159 * is incompatible with the specified collection
160 * (<a href="Collection.html#optional-restrictions">optional</a>)
161 * @throws NullPointerException if this set contains a null element and the
162 * specified collection does not permit null elements
163 * (<a href="Collection.html#optional-restrictions">optional</a>),
164 * or if the specified collection is null
165 * @see #remove(Object)
166 * @see #contains(Object)
167 */
168 public boolean removeAll(Collection<?> c) {
169 boolean modified = false;
170
171 if (size() > c.size()) {
172 for (Iterator<?> i = c.iterator(); i.hasNext(); )
173 modified |= remove(i.next());
174 } else {
175 for (Iterator<?> i = iterator(); i.hasNext(); ) {
176 if (c.contains(i.next())) {
177 i.remove();
178 modified = true;
179 }
180 }
181 }
182 return modified;
183 }
|