Summarising lambda features
Brian Goetz
brian.goetz at oracle.com
Fri Jun 22 17:00:47 PDT 2012
Just some quick comments.
I would write this example:
Iterable<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Dave");
Iterable<String> filteredNames = names
.filter(e -> e.length() >= 4)
.into(new ArrayList<String>());
for (String name : filteredNames) {
System.out.println(name);
}
instead as
List<String> filteredNames = names.filter(...).into(...);
The whole point of into() is to put the data into a *collection*, where
you can process it outside of the streams library. If all you wanted
was an Iterable, you could drop the into() stage.
As a general rule, if Iterable appears in the client code, someone
probably made a mistake.
Note also the APIs for stream operations (filter/map/forEach) are going
to change (returning a Stream abstraction, which will be more like an
Iterator than an Iterable), whereas the client code invocations for
using them will not. So that's two reasons to not use Iterable in these
examples.
I would change "Elements are computed lazily" to "Elements *may be*
computed lazily", as the next round defers the choice of lazy/eager
until it sees the end of the pipeline, and knows whether you are
consuming the whole stream or not. If so, there are more efficient
means of traversal than iterators.
Internal iteration doesn't just hide implementation decisions; it gives
implementations flexibility to use laziness, out-of-order, parallelism,
or whatever other trick it deems effective.
Method references are a bigger deal than your examples suggest. There
are lots of mappers/predicates/sinks that are already represented as
methods:
strings.map(String::length).forEach(numbers::add);
(Where "strings" and "numbers" are collections.)
On 6/22/2012 7:49 PM, James Shaw wrote:
> Having lurked on this list for a while, after reading Brian Goetz's State
> of the Lambda articles, and having struggled with javac myself over several
> evenings, I've had a stab at summarizing the new features available in the
> lambda binary build.
>
> http://datumedge.blogspot.co.uk/2012/06/java-8-lambdas.html
>
> I hope some folks find this useful. I've tried to be as accurate as I can
> within the limitations of my knowledge; if there are glaring mistakes, do
> tell me ;-)
>
More information about the lambda-dev
mailing list