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

Print this page




  63      * Sole constructor.  (For invocation by subclass constructors, typically
  64      * implicit.)
  65      */
  66     protected AbstractCollection() {
  67     }
  68 
  69     // Query Operations
  70 
  71     /**
  72      * Returns an iterator over the elements contained in this collection.
  73      *
  74      * @return an iterator over the elements contained in this collection
  75      */
  76     public abstract Iterator<E> iterator();
  77 
  78     public abstract int size();
  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      *
 127      *  <pre> {@code
 128      * List<E> list = new ArrayList<E>(size());
 129      * for (E e : this)
 130      *     list.add(e);
 131      * return list.toArray();
 132      * }</pre>
 133      */
 134     public Object[] toArray() {
 135         // Estimate size of array; be prepared to see more or fewer elements
 136         Object[] r = new Object[size()];
 137         Iterator<E> it = iterator();
 138         for (int i = 0; i < r.length; i++) {
 139             if (! it.hasNext()) // fewer elements than expected
 140                 return Arrays.copyOf(r, i);
 141             r[i] = it.next();
 142         }
 143         return it.hasNext() ? finishToArray(r, it) : r;
 144     }
 145 
 146     /**
 147      * {@inheritDoc}
 148      *
 149      * <p>This implementation returns an array containing all the elements

 150      * returned by this collection's iterator in the same order, stored in
 151      * consecutive elements of the array, starting with index {@code 0}.
 152      * If the number of elements returned by the iterator is too large to
 153      * fit into the specified array, then the elements are returned in a
 154      * newly allocated array with length equal to the number of elements
 155      * returned by the iterator, even if the size of this collection
 156      * changes during iteration, as might happen if the collection permits
 157      * concurrent modification during iteration.  The {@code size} method is
 158      * called only as an optimization hint; the correct result is returned
 159      * even if the iterator returns a different number of elements.
 160      *
 161      * <p>This method is equivalent to:
 162      *
 163      *  <pre> {@code
 164      * List<E> list = new ArrayList<E>(size());
 165      * for (E e : this)
 166      *     list.add(e);
 167      * return list.toArray(a);
 168      * }</pre>
 169      *


 232             r[i++] = (T)it.next();
 233         }
 234         // trim if overallocated
 235         return (i == r.length) ? r : Arrays.copyOf(r, i);
 236     }
 237 
 238     private static int hugeCapacity(int minCapacity) {
 239         if (minCapacity < 0) // overflow
 240             throw new OutOfMemoryError
 241                 ("Required array size too large");
 242         return (minCapacity > MAX_ARRAY_SIZE) ?
 243             Integer.MAX_VALUE :
 244             MAX_ARRAY_SIZE;
 245     }
 246 
 247     // Modification Operations
 248 
 249     /**
 250      * {@inheritDoc}
 251      *
 252      * <p>This implementation always throws an

 253      * <tt>UnsupportedOperationException</tt>.
 254      *
 255      * @throws UnsupportedOperationException {@inheritDoc}
 256      * @throws ClassCastException            {@inheritDoc}
 257      * @throws NullPointerException          {@inheritDoc}
 258      * @throws IllegalArgumentException      {@inheritDoc}
 259      * @throws IllegalStateException         {@inheritDoc}
 260      */
 261     public boolean add(E e) {
 262         throw new UnsupportedOperationException();
 263     }
 264 
 265     /**
 266      * {@inheritDoc}
 267      *
 268      * <p>This implementation iterates over the collection looking for the

 269      * specified element.  If it finds the element, it removes the element
 270      * from the collection using the iterator's remove method.
 271      *
 272      * <p>Note that this implementation throws an
 273      * <tt>UnsupportedOperationException</tt> if the iterator returned by this
 274      * collection's iterator method does not implement the <tt>remove</tt>
 275      * method and this collection contains the specified object.
 276      *
 277      * @throws UnsupportedOperationException {@inheritDoc}
 278      * @throws ClassCastException            {@inheritDoc}
 279      * @throws NullPointerException          {@inheritDoc}
 280      */
 281     public boolean remove(Object o) {
 282         Iterator<E> it = iterator();
 283         if (o==null) {
 284             while (it.hasNext()) {
 285                 if (it.next()==null) {
 286                     it.remove();
 287                     return true;
 288                 }
 289             }
 290         } else {
 291             while (it.hasNext()) {
 292                 if (o.equals(it.next())) {
 293                     it.remove();
 294                     return true;
 295                 }
 296             }
 297         }
 298         return false;
 299     }
 300 
 301 
 302     // Bulk Operations
 303 
 304     /**
 305      * {@inheritDoc}
 306      *
 307      * <p>This implementation iterates over the specified collection,

 308      * checking each element returned by the iterator in turn to see
 309      * if it's contained in this collection.  If all elements are so
 310      * contained <tt>true</tt> is returned, otherwise <tt>false</tt>.
 311      *
 312      * @throws ClassCastException            {@inheritDoc}
 313      * @throws NullPointerException          {@inheritDoc}
 314      * @see #contains(Object)
 315      */
 316     public boolean containsAll(Collection<?> c) {
 317         for (Object e : c)
 318             if (!contains(e))
 319                 return false;
 320         return true;
 321     }
 322 
 323     /**
 324      * {@inheritDoc}
 325      *
 326      * <p>This implementation iterates over the specified collection, and adds

 327      * each object returned by the iterator to this collection, in turn.
 328      *
 329      * <p>Note that this implementation will throw an
 330      * <tt>UnsupportedOperationException</tt> unless <tt>add</tt> is
 331      * overridden (assuming the specified collection is non-empty).
 332      *
 333      * @throws UnsupportedOperationException {@inheritDoc}
 334      * @throws ClassCastException            {@inheritDoc}
 335      * @throws NullPointerException          {@inheritDoc}
 336      * @throws IllegalArgumentException      {@inheritDoc}
 337      * @throws IllegalStateException         {@inheritDoc}
 338      *
 339      * @see #add(Object)
 340      */
 341     public boolean addAll(Collection<? extends E> c) {
 342         boolean modified = false;
 343         for (E e : c)
 344             if (add(e))
 345                 modified = true;
 346         return modified;
 347     }
 348 
 349     /**
 350      * {@inheritDoc}
 351      *
 352      * <p>This implementation iterates over this collection, checking each

 353      * element returned by the iterator in turn to see if it's contained
 354      * in the specified collection.  If it's so contained, it's removed from
 355      * this collection with the iterator's <tt>remove</tt> method.
 356      *
 357      * <p>Note that this implementation will throw an
 358      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 359      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 360      * and this collection contains one or more elements in common with the
 361      * specified collection.
 362      *
 363      * @throws UnsupportedOperationException {@inheritDoc}
 364      * @throws ClassCastException            {@inheritDoc}
 365      * @throws NullPointerException          {@inheritDoc}
 366      *
 367      * @see #remove(Object)
 368      * @see #contains(Object)
 369      */
 370     public boolean removeAll(Collection<?> c) {
 371         Objects.requireNonNull(c);
 372         boolean modified = false;
 373         Iterator<?> it = iterator();
 374         while (it.hasNext()) {
 375             if (c.contains(it.next())) {
 376                 it.remove();
 377                 modified = true;
 378             }
 379         }
 380         return modified;
 381     }
 382 
 383     /**
 384      * {@inheritDoc}
 385      *
 386      * <p>This implementation iterates over this collection, checking each

 387      * element returned by the iterator in turn to see if it's contained
 388      * in the specified collection.  If it's not so contained, it's removed
 389      * from this collection with the iterator's <tt>remove</tt> method.
 390      *
 391      * <p>Note that this implementation will throw an
 392      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 393      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 394      * and this collection contains one or more elements not present in the
 395      * specified collection.
 396      *
 397      * @throws UnsupportedOperationException {@inheritDoc}
 398      * @throws ClassCastException            {@inheritDoc}
 399      * @throws NullPointerException          {@inheritDoc}
 400      *
 401      * @see #remove(Object)
 402      * @see #contains(Object)
 403      */
 404     public boolean retainAll(Collection<?> c) {
 405         Objects.requireNonNull(c);
 406         boolean modified = false;
 407         Iterator<E> it = iterator();
 408         while (it.hasNext()) {
 409             if (!c.contains(it.next())) {
 410                 it.remove();
 411                 modified = true;
 412             }
 413         }
 414         return modified;
 415     }
 416 
 417     /**
 418      * {@inheritDoc}
 419      *
 420      * <p>This implementation iterates over this collection, removing each

 421      * element using the <tt>Iterator.remove</tt> operation.  Most
 422      * implementations will probably choose to override this method for
 423      * efficiency.
 424      *
 425      * <p>Note that this implementation will throw an
 426      * <tt>UnsupportedOperationException</tt> if the iterator returned by this
 427      * collection's <tt>iterator</tt> method does not implement the
 428      * <tt>remove</tt> method and this collection is non-empty.
 429      *
 430      * @throws UnsupportedOperationException {@inheritDoc}
 431      */
 432     public void clear() {
 433         Iterator<E> it = iterator();
 434         while (it.hasNext()) {
 435             it.next();
 436             it.remove();
 437         }
 438     }
 439 
 440 




  63      * Sole constructor.  (For invocation by subclass constructors, typically
  64      * implicit.)
  65      */
  66     protected AbstractCollection() {
  67     }
  68 
  69     // Query Operations
  70 
  71     /**
  72      * Returns an iterator over the elements contained in this collection.
  73      *
  74      * @return an iterator over the elements contained in this collection
  75      */
  76     public abstract Iterator<E> iterator();
  77 
  78     public abstract int size();
  79 
  80     /**
  81      * {@inheritDoc}
  82      *
  83      * @implSpec
  84      * This implementation returns <tt>size() == 0</tt>.
  85      */
  86     public boolean isEmpty() {
  87         return size() == 0;
  88     }
  89 
  90     /**
  91      * {@inheritDoc}
  92      *
  93      * @implSpec
  94      * This implementation iterates over the elements in the collection,
  95      * checking each element in turn for equality with the specified element.
  96      *
  97      * @throws ClassCastException   {@inheritDoc}
  98      * @throws NullPointerException {@inheritDoc}
  99      */
 100     public boolean contains(Object o) {
 101         Iterator<E> it = iterator();
 102         if (o==null) {
 103             while (it.hasNext())
 104                 if (it.next()==null)
 105                     return true;
 106         } else {
 107             while (it.hasNext())
 108                 if (o.equals(it.next()))
 109                     return true;
 110         }
 111         return false;
 112     }
 113 
 114     /**
 115      * {@inheritDoc}
 116      *
 117      * @implSpec
 118      * This implementation returns an array containing all the elements
 119      * returned by this collection's iterator, in the same order, stored in
 120      * consecutive elements of the array, starting with index {@code 0}.
 121      * The length of the returned array is equal to the number of elements
 122      * returned by the iterator, even if the size of this collection changes
 123      * during iteration, as might happen if the collection permits
 124      * concurrent modification during iteration.  The {@code size} method is
 125      * called only as an optimization hint; the correct result is returned
 126      * even if the iterator returns a different number of elements.
 127      *
 128      * <p>This method is equivalent to:
 129      *
 130      *  <pre> {@code
 131      * List<E> list = new ArrayList<E>(size());
 132      * for (E e : this)
 133      *     list.add(e);
 134      * return list.toArray();
 135      * }</pre>
 136      */
 137     public Object[] toArray() {
 138         // Estimate size of array; be prepared to see more or fewer elements
 139         Object[] r = new Object[size()];
 140         Iterator<E> it = iterator();
 141         for (int i = 0; i < r.length; i++) {
 142             if (! it.hasNext()) // fewer elements than expected
 143                 return Arrays.copyOf(r, i);
 144             r[i] = it.next();
 145         }
 146         return it.hasNext() ? finishToArray(r, it) : r;
 147     }
 148 
 149     /**
 150      * {@inheritDoc}
 151      *
 152      * @implSpec
 153      * This implementation returns an array containing all the elements
 154      * returned by this collection's iterator in the same order, stored in
 155      * consecutive elements of the array, starting with index {@code 0}.
 156      * If the number of elements returned by the iterator is too large to
 157      * fit into the specified array, then the elements are returned in a
 158      * newly allocated array with length equal to the number of elements
 159      * returned by the iterator, even if the size of this collection
 160      * changes during iteration, as might happen if the collection permits
 161      * concurrent modification during iteration.  The {@code size} method is
 162      * called only as an optimization hint; the correct result is returned
 163      * even if the iterator returns a different number of elements.
 164      *
 165      * <p>This method is equivalent to:
 166      *
 167      *  <pre> {@code
 168      * List<E> list = new ArrayList<E>(size());
 169      * for (E e : this)
 170      *     list.add(e);
 171      * return list.toArray(a);
 172      * }</pre>
 173      *


 236             r[i++] = (T)it.next();
 237         }
 238         // trim if overallocated
 239         return (i == r.length) ? r : Arrays.copyOf(r, i);
 240     }
 241 
 242     private static int hugeCapacity(int minCapacity) {
 243         if (minCapacity < 0) // overflow
 244             throw new OutOfMemoryError
 245                 ("Required array size too large");
 246         return (minCapacity > MAX_ARRAY_SIZE) ?
 247             Integer.MAX_VALUE :
 248             MAX_ARRAY_SIZE;
 249     }
 250 
 251     // Modification Operations
 252 
 253     /**
 254      * {@inheritDoc}
 255      *
 256      * @implSpec
 257      * This implementation always throws an
 258      * <tt>UnsupportedOperationException</tt>.
 259      *
 260      * @throws UnsupportedOperationException {@inheritDoc}
 261      * @throws ClassCastException            {@inheritDoc}
 262      * @throws NullPointerException          {@inheritDoc}
 263      * @throws IllegalArgumentException      {@inheritDoc}
 264      * @throws IllegalStateException         {@inheritDoc}
 265      */
 266     public boolean add(E e) {
 267         throw new UnsupportedOperationException();
 268     }
 269 
 270     /**
 271      * {@inheritDoc}
 272      *
 273      * @implSpec
 274      * This implementation iterates over the collection looking for the
 275      * specified element.  If it finds the element, it removes the element
 276      * from the collection using the iterator's remove method.
 277      *
 278      * <p>Note that this implementation throws an
 279      * <tt>UnsupportedOperationException</tt> if the iterator returned by this
 280      * collection's iterator method does not implement the <tt>remove</tt>
 281      * method and this collection contains the specified object.
 282      *
 283      * @throws UnsupportedOperationException {@inheritDoc}
 284      * @throws ClassCastException            {@inheritDoc}
 285      * @throws NullPointerException          {@inheritDoc}
 286      */
 287     public boolean remove(Object o) {
 288         Iterator<E> it = iterator();
 289         if (o==null) {
 290             while (it.hasNext()) {
 291                 if (it.next()==null) {
 292                     it.remove();
 293                     return true;
 294                 }
 295             }
 296         } else {
 297             while (it.hasNext()) {
 298                 if (o.equals(it.next())) {
 299                     it.remove();
 300                     return true;
 301                 }
 302             }
 303         }
 304         return false;
 305     }
 306 
 307 
 308     // Bulk Operations
 309 
 310     /**
 311      * {@inheritDoc}
 312      *
 313      * @implSpec
 314      * This implementation iterates over the specified collection,
 315      * checking each element returned by the iterator in turn to see
 316      * if it's contained in this collection.  If all elements are so
 317      * contained <tt>true</tt> is returned, otherwise <tt>false</tt>.
 318      *
 319      * @throws ClassCastException            {@inheritDoc}
 320      * @throws NullPointerException          {@inheritDoc}
 321      * @see #contains(Object)
 322      */
 323     public boolean containsAll(Collection<?> c) {
 324         for (Object e : c)
 325             if (!contains(e))
 326                 return false;
 327         return true;
 328     }
 329 
 330     /**
 331      * {@inheritDoc}
 332      *
 333      * @implSpec
 334      * This implementation iterates over the specified collection, and adds
 335      * each object returned by the iterator to this collection, in turn.
 336      *
 337      * <p>Note that this implementation will throw an
 338      * <tt>UnsupportedOperationException</tt> unless <tt>add</tt> is
 339      * overridden (assuming the specified collection is non-empty).
 340      *
 341      * @throws UnsupportedOperationException {@inheritDoc}
 342      * @throws ClassCastException            {@inheritDoc}
 343      * @throws NullPointerException          {@inheritDoc}
 344      * @throws IllegalArgumentException      {@inheritDoc}
 345      * @throws IllegalStateException         {@inheritDoc}
 346      *
 347      * @see #add(Object)
 348      */
 349     public boolean addAll(Collection<? extends E> c) {
 350         boolean modified = false;
 351         for (E e : c)
 352             if (add(e))
 353                 modified = true;
 354         return modified;
 355     }
 356 
 357     /**
 358      * {@inheritDoc}
 359      *
 360      * @implSpec
 361      * This implementation iterates over this collection, checking each
 362      * element returned by the iterator in turn to see if it's contained
 363      * in the specified collection.  If it's so contained, it's removed from
 364      * this collection with the iterator's <tt>remove</tt> method.
 365      *
 366      * <p>Note that this implementation will throw an
 367      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 368      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 369      * and this collection contains one or more elements in common with the
 370      * specified collection.
 371      *
 372      * @throws UnsupportedOperationException {@inheritDoc}
 373      * @throws ClassCastException            {@inheritDoc}
 374      * @throws NullPointerException          {@inheritDoc}
 375      *
 376      * @see #remove(Object)
 377      * @see #contains(Object)
 378      */
 379     public boolean removeAll(Collection<?> c) {
 380         Objects.requireNonNull(c);
 381         boolean modified = false;
 382         Iterator<?> it = iterator();
 383         while (it.hasNext()) {
 384             if (c.contains(it.next())) {
 385                 it.remove();
 386                 modified = true;
 387             }
 388         }
 389         return modified;
 390     }
 391 
 392     /**
 393      * {@inheritDoc}
 394      *
 395      * @implSpec
 396      * This implementation iterates over this collection, checking each
 397      * element returned by the iterator in turn to see if it's contained
 398      * in the specified collection.  If it's not so contained, it's removed
 399      * from this collection with the iterator's <tt>remove</tt> method.
 400      *
 401      * <p>Note that this implementation will throw an
 402      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 403      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 404      * and this collection contains one or more elements not present in the
 405      * specified collection.
 406      *
 407      * @throws UnsupportedOperationException {@inheritDoc}
 408      * @throws ClassCastException            {@inheritDoc}
 409      * @throws NullPointerException          {@inheritDoc}
 410      *
 411      * @see #remove(Object)
 412      * @see #contains(Object)
 413      */
 414     public boolean retainAll(Collection<?> c) {
 415         Objects.requireNonNull(c);
 416         boolean modified = false;
 417         Iterator<E> it = iterator();
 418         while (it.hasNext()) {
 419             if (!c.contains(it.next())) {
 420                 it.remove();
 421                 modified = true;
 422             }
 423         }
 424         return modified;
 425     }
 426 
 427     /**
 428      * {@inheritDoc}
 429      *
 430      * @implSpec
 431      * This implementation iterates over this collection, removing each
 432      * element using the <tt>Iterator.remove</tt> operation.  Most
 433      * implementations will probably choose to override this method for
 434      * efficiency.
 435      *
 436      * <p>Note that this implementation will throw an
 437      * <tt>UnsupportedOperationException</tt> if the iterator returned by this
 438      * collection's <tt>iterator</tt> method does not implement the
 439      * <tt>remove</tt> method and this collection is non-empty.
 440      *
 441      * @throws UnsupportedOperationException {@inheritDoc}
 442      */
 443     public void clear() {
 444         Iterator<E> it = iterator();
 445         while (it.hasNext()) {
 446             it.next();
 447             it.remove();
 448         }
 449     }
 450 
 451