Additional method on Stream
Stephen Colebourne
scolebourne at joda.org
Mon Apr 27 13:22:49 UTC 2015
This is a request for an additional method on java.util.stream.Stream.
Having written quite a bit of code in Java 8 now, one annoyance keeps
on coming up, filtering and casting the stream.
Currently, it is necessary to do this:
return input.stream()
.filter(Foo.class::isInstance)
.map(Foo.class::cast)
.someTerminalOperation();
or
return input.stream()
.filter(t -> t instanceof Foo)
.map(t -> (Foo) t)
.someTerminalOperation();
Really, what is needed is this:
return input.stream()
.filter(Foo.class)
.someTerminalOperation();
For info, Guava's FluentIterable has such a method.
The new method signature would be something like:
public Stream<R> filter(Class<R> cls);
As far as I can see, there is no problem in implementing this in both
serial and parallel modes, as it is essentially just a convenience.
Thoughts?
Stephen
More information about the core-libs-dev
mailing list