src/share/classes/java/util/AbstractCollection.java

Print this page




  79 
  80     /**
  81      * {@inheritDoc}
  82      *
  83      * <p>This implementation returns <tt>size() == 0</tt>.
  84      */
  85     public boolean isEmpty() {
  86         return size() == 0;
  87     }
  88 
  89     /**
  90      * {@inheritDoc}
  91      *
  92      * <p>This implementation iterates over the elements in the collection,
  93      * checking each element in turn for equality with the specified element.
  94      *
  95      * @throws ClassCastException   {@inheritDoc}
  96      * @throws NullPointerException {@inheritDoc}
  97      */
  98     public boolean contains(Object o) {
  99         Iterator<E> e = iterator();
 100         if (o==null) {
 101             while (e.hasNext())
 102                 if (e.next()==null)
 103                     return true;
 104         } else {
 105             while (e.hasNext())
 106                 if (o.equals(e.next()))
 107                     return true;
 108         }
 109         return false;
 110     }
 111 
 112     /**
 113      * {@inheritDoc}
 114      *
 115      * <p>This implementation returns an array containing all the elements
 116      * returned by this collection's iterator, in the same order, stored in
 117      * consecutive elements of the array, starting with index {@code 0}.
 118      * The length of the returned array is equal to the number of elements
 119      * returned by the iterator, even if the size of this collection changes
 120      * during iteration, as might happen if the collection permits
 121      * concurrent modification during iteration.  The {@code size} method is
 122      * called only as an optimization hint; the correct result is returned
 123      * even if the iterator returns a different number of elements.
 124      *
 125      * <p>This method is equivalent to:
 126      *


 252         throw new UnsupportedOperationException();
 253     }
 254 
 255     /**
 256      * {@inheritDoc}
 257      *
 258      * <p>This implementation iterates over the collection looking for the
 259      * specified element.  If it finds the element, it removes the element
 260      * from the collection using the iterator's remove method.
 261      *
 262      * <p>Note that this implementation throws an
 263      * <tt>UnsupportedOperationException</tt> if the iterator returned by this
 264      * collection's iterator method does not implement the <tt>remove</tt>
 265      * method and this collection contains the specified object.
 266      *
 267      * @throws UnsupportedOperationException {@inheritDoc}
 268      * @throws ClassCastException            {@inheritDoc}
 269      * @throws NullPointerException          {@inheritDoc}
 270      */
 271     public boolean remove(Object o) {
 272         Iterator<E> e = iterator();
 273         if (o==null) {
 274             while (e.hasNext()) {
 275                 if (e.next()==null) {
 276                     e.remove();
 277                     return true;
 278                 }
 279             }
 280         } else {
 281             while (e.hasNext()) {
 282                 if (o.equals(e.next())) {
 283                     e.remove();
 284                     return true;
 285                 }
 286             }
 287         }
 288         return false;
 289     }
 290 
 291 
 292     // Bulk Operations
 293 
 294     /**
 295      * {@inheritDoc}
 296      *
 297      * <p>This implementation iterates over the specified collection,
 298      * checking each element returned by the iterator in turn to see
 299      * if it's contained in this collection.  If all elements are so
 300      * contained <tt>true</tt> is returned, otherwise <tt>false</tt>.
 301      *
 302      * @throws ClassCastException            {@inheritDoc}
 303      * @throws NullPointerException          {@inheritDoc}
 304      * @see #contains(Object)
 305      */
 306     public boolean containsAll(Collection<?> c) {
 307         Iterator<?> e = c.iterator();
 308         while (e.hasNext())
 309             if (!contains(e.next()))
 310                 return false;
 311         return true;
 312     }
 313 
 314     /**
 315      * {@inheritDoc}
 316      *
 317      * <p>This implementation iterates over the specified collection, and adds
 318      * each object returned by the iterator to this collection, in turn.
 319      *
 320      * <p>Note that this implementation will throw an
 321      * <tt>UnsupportedOperationException</tt> unless <tt>add</tt> is
 322      * overridden (assuming the specified collection is non-empty).
 323      *
 324      * @throws UnsupportedOperationException {@inheritDoc}
 325      * @throws ClassCastException            {@inheritDoc}
 326      * @throws NullPointerException          {@inheritDoc}
 327      * @throws IllegalArgumentException      {@inheritDoc}
 328      * @throws IllegalStateException         {@inheritDoc}
 329      *
 330      * @see #add(Object)
 331      */
 332     public boolean addAll(Collection<? extends E> c) {
 333         boolean modified = false;
 334         Iterator<? extends E> e = c.iterator();
 335         while (e.hasNext()) {
 336             if (add(e.next()))
 337                 modified = true;
 338         }
 339         return modified;
 340     }
 341 
 342     /**
 343      * {@inheritDoc}
 344      *
 345      * <p>This implementation iterates over this collection, checking each
 346      * element returned by the iterator in turn to see if it's contained
 347      * in the specified collection.  If it's so contained, it's removed from
 348      * this collection with the iterator's <tt>remove</tt> method.
 349      *
 350      * <p>Note that this implementation will throw an
 351      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 352      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 353      * and this collection contains one or more elements in common with the
 354      * specified collection.
 355      *
 356      * @throws UnsupportedOperationException {@inheritDoc}
 357      * @throws ClassCastException            {@inheritDoc}
 358      * @throws NullPointerException          {@inheritDoc}
 359      *
 360      * @see #remove(Object)
 361      * @see #contains(Object)
 362      */
 363     public boolean removeAll(Collection<?> c) {
 364         boolean modified = false;
 365         Iterator<?> e = iterator();
 366         while (e.hasNext()) {
 367             if (c.contains(e.next())) {
 368                 e.remove();
 369                 modified = true;
 370             }
 371         }
 372         return modified;
 373     }
 374 
 375     /**
 376      * {@inheritDoc}
 377      *
 378      * <p>This implementation iterates over this collection, checking each
 379      * element returned by the iterator in turn to see if it's contained
 380      * in the specified collection.  If it's not so contained, it's removed
 381      * from this collection with the iterator's <tt>remove</tt> method.
 382      *
 383      * <p>Note that this implementation will throw an
 384      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 385      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 386      * and this collection contains one or more elements not present in the
 387      * specified collection.
 388      *
 389      * @throws UnsupportedOperationException {@inheritDoc}
 390      * @throws ClassCastException            {@inheritDoc}
 391      * @throws NullPointerException          {@inheritDoc}
 392      *
 393      * @see #remove(Object)
 394      * @see #contains(Object)
 395      */
 396     public boolean retainAll(Collection<?> c) {
 397         boolean modified = false;
 398         Iterator<E> e = iterator();
 399         while (e.hasNext()) {
 400             if (!c.contains(e.next())) {
 401                 e.remove();
 402                 modified = true;
 403             }
 404         }
 405         return modified;
 406     }
 407 
 408     /**
 409      * {@inheritDoc}
 410      *
 411      * <p>This implementation iterates over this collection, removing each
 412      * element using the <tt>Iterator.remove</tt> operation.  Most
 413      * implementations will probably choose to override this method for
 414      * efficiency.
 415      *
 416      * <p>Note that this implementation will throw an
 417      * <tt>UnsupportedOperationException</tt> if the iterator returned by this
 418      * collection's <tt>iterator</tt> method does not implement the
 419      * <tt>remove</tt> method and this collection is non-empty.
 420      *
 421      * @throws UnsupportedOperationException {@inheritDoc}
 422      */
 423     public void clear() {
 424         Iterator<E> e = iterator();
 425         while (e.hasNext()) {
 426             e.next();
 427             e.remove();
 428         }
 429     }
 430 
 431 
 432     //  String conversion
 433 
 434     /**
 435      * Returns a string representation of this collection.  The string
 436      * representation consists of a list of the collection's elements in the
 437      * order they are returned by its iterator, enclosed in square brackets
 438      * (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
 439      * <tt>", "</tt> (comma and space).  Elements are converted to strings as
 440      * by {@link String#valueOf(Object)}.
 441      *
 442      * @return a string representation of this collection
 443      */
 444     public String toString() {
 445         Iterator<E> i = iterator();
 446         if (! i.hasNext())
 447             return "[]";
 448 
 449         StringBuilder sb = new StringBuilder();
 450         sb.append('[');
 451         for (;;) {
 452             E e = i.next();
 453             sb.append(e == this ? "(this Collection)" : e);
 454             if (! i.hasNext())
 455                 return sb.append(']').toString();
 456             sb.append(", ");
 457         }
 458     }
 459 
 460 }


  79 
  80     /**
  81      * {@inheritDoc}
  82      *
  83      * <p>This implementation returns <tt>size() == 0</tt>.
  84      */
  85     public boolean isEmpty() {
  86         return size() == 0;
  87     }
  88 
  89     /**
  90      * {@inheritDoc}
  91      *
  92      * <p>This implementation iterates over the elements in the collection,
  93      * checking each element in turn for equality with the specified element.
  94      *
  95      * @throws ClassCastException   {@inheritDoc}
  96      * @throws NullPointerException {@inheritDoc}
  97      */
  98     public boolean contains(Object o) {
  99         Iterator<E> it = iterator();
 100         if (o==null) {
 101             while (it.hasNext())
 102                 if (it.next()==null)
 103                     return true;
 104         } else {
 105             while (it.hasNext())
 106                 if (o.equals(it.next()))
 107                     return true;
 108         }
 109         return false;
 110     }
 111 
 112     /**
 113      * {@inheritDoc}
 114      *
 115      * <p>This implementation returns an array containing all the elements
 116      * returned by this collection's iterator, in the same order, stored in
 117      * consecutive elements of the array, starting with index {@code 0}.
 118      * The length of the returned array is equal to the number of elements
 119      * returned by the iterator, even if the size of this collection changes
 120      * during iteration, as might happen if the collection permits
 121      * concurrent modification during iteration.  The {@code size} method is
 122      * called only as an optimization hint; the correct result is returned
 123      * even if the iterator returns a different number of elements.
 124      *
 125      * <p>This method is equivalent to:
 126      *


 252         throw new UnsupportedOperationException();
 253     }
 254 
 255     /**
 256      * {@inheritDoc}
 257      *
 258      * <p>This implementation iterates over the collection looking for the
 259      * specified element.  If it finds the element, it removes the element
 260      * from the collection using the iterator's remove method.
 261      *
 262      * <p>Note that this implementation throws an
 263      * <tt>UnsupportedOperationException</tt> if the iterator returned by this
 264      * collection's iterator method does not implement the <tt>remove</tt>
 265      * method and this collection contains the specified object.
 266      *
 267      * @throws UnsupportedOperationException {@inheritDoc}
 268      * @throws ClassCastException            {@inheritDoc}
 269      * @throws NullPointerException          {@inheritDoc}
 270      */
 271     public boolean remove(Object o) {
 272         Iterator<E> it = iterator();
 273         if (o==null) {
 274             while (it.hasNext()) {
 275                 if (it.next()==null) {
 276                     it.remove();
 277                     return true;
 278                 }
 279             }
 280         } else {
 281             while (it.hasNext()) {
 282                 if (o.equals(it.next())) {
 283                     it.remove();
 284                     return true;
 285                 }
 286             }
 287         }
 288         return false;
 289     }
 290 
 291 
 292     // Bulk Operations
 293 
 294     /**
 295      * {@inheritDoc}
 296      *
 297      * <p>This implementation iterates over the specified collection,
 298      * checking each element returned by the iterator in turn to see
 299      * if it's contained in this collection.  If all elements are so
 300      * contained <tt>true</tt> is returned, otherwise <tt>false</tt>.
 301      *
 302      * @throws ClassCastException            {@inheritDoc}
 303      * @throws NullPointerException          {@inheritDoc}
 304      * @see #contains(Object)
 305      */
 306     public boolean containsAll(Collection<?> c) {
 307         for (Object e : c)
 308             if (!contains(e))

 309                 return false;
 310         return true;
 311     }
 312 
 313     /**
 314      * {@inheritDoc}
 315      *
 316      * <p>This implementation iterates over the specified collection, and adds
 317      * each object returned by the iterator to this collection, in turn.
 318      *
 319      * <p>Note that this implementation will throw an
 320      * <tt>UnsupportedOperationException</tt> unless <tt>add</tt> is
 321      * overridden (assuming the specified collection is non-empty).
 322      *
 323      * @throws UnsupportedOperationException {@inheritDoc}
 324      * @throws ClassCastException            {@inheritDoc}
 325      * @throws NullPointerException          {@inheritDoc}
 326      * @throws IllegalArgumentException      {@inheritDoc}
 327      * @throws IllegalStateException         {@inheritDoc}
 328      *
 329      * @see #add(Object)
 330      */
 331     public boolean addAll(Collection<? extends E> c) {
 332         boolean modified = false;
 333         for (E e : c)
 334             if (add(e))

 335                 modified = true;

 336         return modified;
 337     }
 338 
 339     /**
 340      * {@inheritDoc}
 341      *
 342      * <p>This implementation iterates over this collection, checking each
 343      * element returned by the iterator in turn to see if it's contained
 344      * in the specified collection.  If it's so contained, it's removed from
 345      * this collection with the iterator's <tt>remove</tt> method.
 346      *
 347      * <p>Note that this implementation will throw an
 348      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 349      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 350      * and this collection contains one or more elements in common with the
 351      * specified collection.
 352      *
 353      * @throws UnsupportedOperationException {@inheritDoc}
 354      * @throws ClassCastException            {@inheritDoc}
 355      * @throws NullPointerException          {@inheritDoc}
 356      *
 357      * @see #remove(Object)
 358      * @see #contains(Object)
 359      */
 360     public boolean removeAll(Collection<?> c) {
 361         boolean modified = false;
 362         Iterator<?> it = iterator();
 363         while (it.hasNext()) {
 364             if (c.contains(it.next())) {
 365                 it.remove();
 366                 modified = true;
 367             }
 368         }
 369         return modified;
 370     }
 371 
 372     /**
 373      * {@inheritDoc}
 374      *
 375      * <p>This implementation iterates over this collection, checking each
 376      * element returned by the iterator in turn to see if it's contained
 377      * in the specified collection.  If it's not so contained, it's removed
 378      * from this collection with the iterator's <tt>remove</tt> method.
 379      *
 380      * <p>Note that this implementation will throw an
 381      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 382      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 383      * and this collection contains one or more elements not present in the
 384      * specified collection.
 385      *
 386      * @throws UnsupportedOperationException {@inheritDoc}
 387      * @throws ClassCastException            {@inheritDoc}
 388      * @throws NullPointerException          {@inheritDoc}
 389      *
 390      * @see #remove(Object)
 391      * @see #contains(Object)
 392      */
 393     public boolean retainAll(Collection<?> c) {
 394         boolean modified = false;
 395         Iterator<E> it = iterator();
 396         while (it.hasNext()) {
 397             if (!c.contains(it.next())) {
 398                 it.remove();
 399                 modified = true;
 400             }
 401         }
 402         return modified;
 403     }
 404 
 405     /**
 406      * {@inheritDoc}
 407      *
 408      * <p>This implementation iterates over this collection, removing each
 409      * element using the <tt>Iterator.remove</tt> operation.  Most
 410      * implementations will probably choose to override this method for
 411      * efficiency.
 412      *
 413      * <p>Note that this implementation will throw an
 414      * <tt>UnsupportedOperationException</tt> if the iterator returned by this
 415      * collection's <tt>iterator</tt> method does not implement the
 416      * <tt>remove</tt> method and this collection is non-empty.
 417      *
 418      * @throws UnsupportedOperationException {@inheritDoc}
 419      */
 420     public void clear() {
 421         Iterator<E> it = iterator();
 422         while (it.hasNext()) {
 423             it.next();
 424             it.remove();
 425         }
 426     }
 427 
 428 
 429     //  String conversion
 430 
 431     /**
 432      * Returns a string representation of this collection.  The string
 433      * representation consists of a list of the collection's elements in the
 434      * order they are returned by its iterator, enclosed in square brackets
 435      * (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
 436      * <tt>", "</tt> (comma and space).  Elements are converted to strings as
 437      * by {@link String#valueOf(Object)}.
 438      *
 439      * @return a string representation of this collection
 440      */
 441     public String toString() {
 442         Iterator<E> it = iterator();
 443         if (! it.hasNext())
 444             return "[]";
 445 
 446         StringBuilder sb = new StringBuilder();
 447         sb.append('[');
 448         for (;;) {
 449             E e = it.next();
 450             sb.append(e == this ? "(this Collection)" : e);
 451             if (! it.hasNext())
 452                 return sb.append(']').toString();
 453             sb.append(',').append(' ');
 454         }
 455     }
 456 
 457 }