-
Enhancement
-
Resolution: Not an Issue
-
P3
-
None
Add something like
default Collection<E> Collection.asUnmodifiable()
as a default method to the Collection interface that returns an unmodifiable-wrapped instance of that Collection. Covariant overrides would be added for all the collection interfaces: List, Set, Map, Sorted{Map,Set}, Navigable{Map,Set}; and also EnumSet and EnumMap.
This makes it easier to chain. For example, consider
List<Type> = input.stream()
.map(foo)
.filter(bar)
.collect(toList());
To make this unmodifiable, one would have to do
List<Type> = Collections.unmodifiableList(input.stream()
.map(foo)
.filter(bar)
.collect(toList()));
or
List<Type> = input.stream()
.map(foo)
.filter(bar)
.collect(collectingAndThen(toList(), Collections::unmodifiableList);
It would be nicer to do
List<Type> = input.stream()
.map(foo)
.filter(bar)
.collect(toList())
.asUnmodifiable();
and chaining additional operations to the end would be easier.
default Collection<E> Collection.asUnmodifiable()
as a default method to the Collection interface that returns an unmodifiable-wrapped instance of that Collection. Covariant overrides would be added for all the collection interfaces: List, Set, Map, Sorted{Map,Set}, Navigable{Map,Set}; and also EnumSet and EnumMap.
This makes it easier to chain. For example, consider
List<Type> = input.stream()
.map(foo)
.filter(bar)
.collect(toList());
To make this unmodifiable, one would have to do
List<Type> = Collections.unmodifiableList(input.stream()
.map(foo)
.filter(bar)
.collect(toList()));
or
List<Type> = input.stream()
.map(foo)
.filter(bar)
.collect(collectingAndThen(toList(), Collections::unmodifiableList);
It would be nicer to do
List<Type> = input.stream()
.map(foo)
.filter(bar)
.collect(toList())
.asUnmodifiable();
and chaining additional operations to the end would be easier.