ArrayList.removeAll()/retainAll()

Louis Wasserman wasserman.louis at gmail.com
Tue Feb 4 17:22:33 UTC 2014


I don't follow.  It looks like ArrayList is throwing an exception on
removeAll(null) -- perfectly valid, in the spec -- not throwing an
exception when the collection contains null.

Louis Wasserman
wasserman.louis at gmail.com
http://profiles.google.com/wasserman.louis


On Tue, Feb 4, 2014 at 9:00 AM, Benedict Elliott Smith <
belliottsmith at datastax.com> wrote:

> Hi,
>
> I notice this (or a related issue) has been mentioned
> before<
> http://mail.openjdk.java.net/pipermail/core-libs-dev/2013-June/017663.html
> >on
> this list, but I'm not convinced the correct resolution was reached.
> We
> are seeing this problem thrown by antlr, but rather than a bug in antlr, as
> surmised on the previous exchange, it looks to me that ArrayList is
> imposing a new constraint that is neither declared by itself nor
> Collection, and is unnecessary. ArrayList happily supports null elements,
> so requiring that the provided collection has no null elements is surely a
> bug?
>
> I've pasted the two declarations below for ease of reference. Neither
> javadocs describe the constraint that is imposed.
>
> ArrayList:
>     /**
>      * Removes from this list all of its elements that are contained in the
>      * specified collection.
>      *
>      * @param c collection containing elements to be removed from this list
>      * @return {@code true} if this list changed as a result of the call
>      * @throws ClassCastException if the class of an element of this list
>      *         is incompatible with the specified collection
>      * (<a href="Collection.html#optional-restrictions">optional</a>)
>      * @throws NullPointerException if this list contains a null element
> and the
>      *         specified collection does not permit null elements
>      * (<a href="Collection.html#optional-restrictions">optional</a>),
>      *         or if the specified collection is null
>      * @see Collection#contains(Object)
>      */
>     public boolean removeAll(Collection<?> c) {
>         Objects.requireNonNull(c);
>         return batchRemove(c, false);
>     }
>
> Collection:
>     /**
>      * Removes all of this collection's elements that are also contained in
> the
>      * specified collection (optional operation).  After this call returns,
>      * this collection will contain no elements in common with the
> specified
>      * collection.
>      *
>      * @param c collection containing elements to be removed from this
> collection
>      * @return <tt>true</tt> if this collection changed as a result of the
>      *         call
>      * @throws UnsupportedOperationException if the <tt>removeAll</tt>
> method
>      *         is not supported by this collection
>      * @throws ClassCastException if the types of one or more elements
>      *         in this collection are incompatible with the specified
>      *         collection
>      *         (<a href="#optional-restrictions">optional</a>)
>      * @throws NullPointerException if this collection contains one or more
>      *         null elements and the specified collection does not support
>      *         null elements
>      *         (<a href="#optional-restrictions">optional</a>),
>      *         or if the specified collection is null
>      * @see #remove(Object)
>      * @see #contains(Object)
>      */
>     boolean removeAll(Collection<?> c);
>



More information about the core-libs-dev mailing list