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 }
184
185 }
|
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 Objects.requireNonNull(c);
170 boolean modified = false;
171
172 if (size() > c.size()) {
173 for (Iterator<?> i = c.iterator(); i.hasNext(); )
174 modified |= remove(i.next());
175 } else {
176 for (Iterator<?> i = iterator(); i.hasNext(); ) {
177 if (c.contains(i.next())) {
178 i.remove();
179 modified = true;
180 }
181 }
182 }
183 return modified;
184 }
185
186 }
|