On 9/22/16 11:13 PM, Kasper Nielsen wrote:
I accidently bumped into an old friend today
https://docs.oracle.com/javase/8/docs/technotes/guides/collections/designfaq.html
There are couple of questions that might need an update after Java 8. Such
as

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
predicate)?

Hi Kasper,

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 to List?

    int findIndexMatching(Predicate<E>)
    int findLastIndexMatching(Predicate<E>)

(doesn't have to be these names, of course)

Or is it sufficient to stream over the list indexes and search that way?

    IntStream.range(0, list.size())
             .filter(i -> predicate.test(list.get(i)))
             .findFirst();

s'marks

Reply via email to