Collection Design FAQ
stuart.marks at oracle.com
Sat Oct 1 00:05:12 UTC 2016
On 9/22/16 11:13 PM, Kasper Nielsen wrote:
> I accidently bumped into an old friend today
> There are couple of questions that might need an update after Java 8. Such
> Why don't you provide an "apply" method in Collection to apply a given
> method ("upcall") to all the elements of the Collection?
> Why didn't you provide a "Predicate" interface, and related methods (e.g.,
> a method to find the first element in the Collection satisfying the
The collection design FAQ is certainly out of date.
Regarding an "apply" method, as Rémi mentioned, there is Iterable.forEach. For
List, there is also List.replaceAll.
It was certainly possible to have a Predicate interface before Java 8, but the
only way to use it would have been to use an anonymous inner class (or a
pre-existing class that implemented it). This would have been pretty cumbersome
to use. Of course, this is all different now that we have lambdas.
It's pretty straightforward to use streams to find the first matching element of
a List, but less so to find the index of that element, like List.indexOf or
List.lastIndexOf. Would it be useful if something like the following were added
(doesn't have to be these names, of course)
Or is it sufficient to stream over the list indexes and search that way?
.filter(i -> predicate.test(list.get(i)))
More information about the core-libs-dev