//java architect /
Exploring Lambda Expressions for
the Java Language and the JVM
With lambda expressions, write code that is more concise and can truly run in parallel.
JAVA IN ACTION
BEN EVANS,
MARTIJN VERBURG,
AND TRISHA GEE
Lambda expressions, a name chosen by the
Expert Group to describe a
new functional programming
construct, are one of the
most eagerly awaited new
features coming in Java SE 8.
You’ll sometimes also hear
people using terms such
as closures, function literals, anonymous functions,
and Single Abstract Methods
(SAMs). Some of these terms
have slightly different meanings from each other, but
they all refer to the same
basic functionality.
Although lambda expressions might seem unfamiliar
to begin with, they’re quite
easy to pick up, and mastering them will be vital for writing applications that can take
full advantage of modern
multicore CPUs.
A key concept to remember
is that lambda expressions
are just small bits of func-
tionality that can be passed
around as data. A secondary
concept that you’ll need to
master is understanding how
collections will be iterated
over internally, as opposed to
the external, serial iteration
we have today.
classes that have only one
function.
For example, if your code
has a large number of anonymous inner classes—
defining things such as listeners
and handlers in UI code or
callables and runnables in
concurrent code—moving
to the lambda expressions
style can make your code
much shorter and easier to
understand.
Ability to modify methods.
Sometimes, methods don’t
quite have the functionality we want. For example,
the contains() method in the
Collection interface returns
true only if the exact object
passed in is present in the
collection. There’s no way to
tweak that functionality, for
example, to have the method
return true if the string we’re
looking for is present but
uses different capitalization.
Loosely, what we want
to do is “pass in some new
code of our own” to an exist-
ing method, which will then
call the code that we passed
in. Lambda expressions can
provide a good way to repre-
sent the code that has been
passed in and should be
called back.
ABOUT US
Why You Want Lambda
Expressions
There are three main reasons why programmers want
lambda expressions:
■ ■ More-concise code
■ ■ Ability to modify methods
by supplying additional
functionality
■ ■ Better support for multicore processing
More-concise code. Lambda
expressions provide a neater
way of implementing Java
blog
PHOTOGRAPHY BY
JOHN BLYTHE (EVANS, GEE)
AND BOB ADLER (VERBURG)