Migrating methods in Collections

Brian Goetz brian.goetz at oracle.com
Thu Dec 24 16:01:22 UTC 2015

Cowardly of you to not put some form of toArray() in AnyCollection :)

I still think the logical signatures here would be:
     E[] toArray()
     <U super E> toArray(IntFunction<U[]> generator)

These conflict with their counterparts in Collection, but in exactly the 
same way as contains/remove do.

Separately, one might wonder (and you should wonder) "What does ? 
extends/super T mean when T is an avar?"

The answer is: another dependent type, namely:

    ? extends T  =  if (ref T)
                        then ? extends T
                        else T

This may look circular, but is not; we already have a definition of "? 
extends T" for a reference tvar.  So when Foo<any T> is instantiated 
with a value type, then the variance on T folds away.

The point is: get used to this reasoning of "If T is a ref, then this, 
otherwise that."  It's everywhere.  (We can choose to be honest about 
it, or we can try and paper over it.)

On 12/24/2015 8:24 AM, Doug Lea wrote:
> You might look at discussions so far as conceptually adding a
> super-interface to Collection, but jamming all the methods
> into Collection itself. It might be easier to pretend otherwise
> for a while. If we had some really good names for these, we might
> even prefer this way.
> Here's sketch of Collection (Not yet for List and Map)
> interface AnyCollection<E> {
>     boolean isEmpty();
>     boolean contains(E e); // not Object
>     boolean add(E e);
>     boolean remove(E e);   // not Object
>     boolean removeIf(Predicate<? super E> filter);
>     void clear();
>     Iterator<E> iterator();
>     Stream<E> stream();
>     Stream<E> parallelStream();
>     boolean addAll(AnyCollection<? extends E> c);
>     boolean containsAll(AnyCollection<? extends E> c); // not 
> Collection<?>
>     boolean removeAll(AnyCollection<? extends E> c);   // not 
> Collection<?>
>     boolean retainAll(AnyCollection<? extends E> c);   // not 
> Collection<?>
>     // to address other issues:
>     long elementCount();   // not size()
>     AnyCollection<E> adding(E e);
>     AnyCollection<E> removing(E e);
> }
> interface Collection<E> extends AnyCollection<E> {
>     int size();
>     boolean contains(Object o); // contravariant arg
>     boolean remove(Object o);   // contravariant arg
>     Object[] toArray();
>     <T> T[] toArray(T[] a);
>     boolean equals(Object o);   // declare for sake of Collection spec
>     int hashCode();             // declare for sake of Collection spec
>     boolean containsAll(Collection<?> c);
>     boolean removeAll(Collection<?> c);
>     boolean retainAll(Collection<?> c);
> }

More information about the valhalla-spec-experts mailing list