Migrating methods in Collections

Doug Lea dl at cs.oswego.edu
Thu Dec 24 16:17:11 UTC 2015

On 12/24/2015 11:01 AM, Brian Goetz wrote:
> Cowardly of you to not put some form of toArray() in AnyCollection :)

My intent was to keep at AnyX level only those things that
would never get you into null, boxing, or erasure trouble.
(Which led to some some jdk8 deja vu of jdk5 deja vu!)
I don't know of best replacement for toArray or
even whether it should be mandated. But I tried pass two
also with List and Map sketches. Not meant as a proposal,
just as a way to help focus on current and upcoming issues.
Including for example Pair<K,V> vs Map.Entry.

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();

     Spliterator<E> spliterator();    // iterator() omitted
     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

     Iterator<E> iterator();

     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(AnyCollection<?> c);
     boolean removeAll(AnyCollection<?> c);
     boolean retainAll(AnyCollection<?> c);

interface AnyList<E> extends AnyCollection<E> {
     void replaceAll(UnaryOperator<E> operator);
     void sort(Comparator<? super E> c);
     E at(long index);
     E setAt(long index, E element);
     void addAt(long index, E element);
     boolean addAllAt(long index, AnyCollection<? extends E> c);
     E removeAt(long index);
     long findFirst(E e);
     long findLast(E e);
     // subList?

interface List<E> extends AnyList<E> {
     E get(int index);
     E set(int index, E element);
     void add(int index, E element);
     boolean addAll(int index, AnyCollection<? extends E> c);
     E remove(int index);
     int indexOf(Object o);
     int lastIndexOf(Object o);
     ListIterator<E> listIterator();
     ListIterator<E> listIterator(long index);
     List<E> subList(int fromIndex, int toIndex);

interface AnyMap<K,V> { // too bad not: extends AnyCollection<Pair<K,V>>
     boolean isEmpty();
     long mappingCount();   // as above; not size()

     boolean containsKey(K key);
     boolean containsValue(V value);

     Optional<V> at(K key);
     V at(K key, V defaultValue);

     boolean putAt(K key, V value);
     V installAt(K key, V value); //  putIfAbsent, but return current val
     boolean replaceAt(K key, V value);
     boolean replaceAt(K key, V oldValue, V newValue);

     boolean removeAt(K key);
     boolean removeAt(K key, V value);
     void clear();

     Spliterator<K> keySpliterator();
     Stream<K> keyStream();
     Spliterator<V> valueSpliterator();
     Stream<V> valueStream();
     Spliterator<Pair<K,V>> spliterator(); // Pair?
     Stream<Pair<K,V>> stream();

     void putAll(AnyMap<? extends K, ? extends V> m);
     void forEach(BiConsumer<? super K, ? super V> action);
     void replaceAll(BiFunction<? super K, ? super V, ? extends V> fun);

     // Might need renaming because of null return as sentinel

     V computeIfAbsent(K key, Function<? super K, ? extends V> fun);
     V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> fun);
     V compute(K key, BiFunction<? super K, ? super V, ? extends V> fun);
     V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> fun);

interface Map<K,V> extends AnyMap<K,V> {
     int size();
     boolean containsKey(Object key);
     boolean containsValue(Object value);
     V get(Object key);
     V getOrDefault(Object key, V defaultValue);
     V put(K key, V value);
     V putIfAbsent(K key, V value);
     V remove(Object key);
     boolean remove(Object key, Object value);
     V replace(K key, V value);

     Set<K> keySet();
     Collection<V> values();
     Set<Map.Entry<K, V>> entrySet();

     boolean equals(Object o);
     int hashCode();

More information about the valhalla-spec-experts mailing list