Removes all objects from this Collection that are not also found in the Collection passed (optional). After this method returns this Collection will only contain elements that also can be found in the Collection passed to this method. This implementation iterates
(Collection<?> collection)
| 313 | * if {@code collection} is {@code null}. |
| 314 | */ |
| 315 | public boolean retainAll(Collection<?> collection) { |
| 316 | boolean result = false; |
| 317 | Iterator<?> it = iterator(); |
| 318 | while (it.hasNext()) { |
| 319 | if (!collection.contains(it.next())) { |
| 320 | it.remove(); |
| 321 | result = true; |
| 322 | } |
| 323 | } |
| 324 | return result; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Returns a count of how many objects this {@code Collection} contains. |