Enhance Map, List, and Set such that these interfaces extend Function, IntFunction, and Predicate, respectively

Brian Goetz brian.goetz at oracle.com
Thu Sep 15 15:34:15 UTC 2022



On 9/15/2022 11:19 AM, Ralf Spöth wrote:
> Dear all,
>
> I just stumbled about some frequent uses of collections as filters and 
> mapping functions, and I am wondering whether it's possible to enhance 
> the core collection interfaces Map, List, and Set:
>
> (please forgive lousy syntax hereinafter)
>
> interface Map<K, V> extends Function<K, V> {
>     default V apply(K k) {return get(k);}
> }
>
> interface List<T> extends IntFunction<T> {
>    default T apply(int index) { return get(I);}
> }
>
> interface Set<T> extends Predicate<T> {
>    default boolean test(T t) {return contains(t);}
> }

These were considered during JSR 335.  They are conceptually sensible, 
but we decided against it for two reasons:

  - Adding these methods to highly general interfaces with many 
implementations extant could introduce conflicts, where someone's 
`MySet` could have an incompatible "test" method;

  - The functionality you want is easy accessed via method references: 
map::get converts to Function (and map::contains to Predicate); 
list::get conversions to IntFunction; set::contains to Predicate.




More information about the java-se-spec-comments mailing list