Download - Lambda Expressions in Java 8
little bit about me.. Worked for the gov side of life for 4 years with ManTech and
SAIC Did it all
Prior to that I was in the Marines got to do a bit of traveling
Now I work here at 5AM Solutions Full stack dev
Interests as of late… Docker
Linux Mint
Cloud-computing concepts
Disclaimers I love Java, but don’t know everything about
Java…especially Java 8.
I’m not an expert.
Feel free to ask any questions at any time. I will do my
best to answer them.
Any questions that I can’t answer, I promise to research
and respond promptly on the MCJUG forum.
Venkat Subramaniam Basis of my talk
Heard him give a talk
over lambda
expressions in Java 8 at
NFJS conference
Venkat’s amazon book
list/bio
What I plan to cover… Lambda expression syntax
Iterate through a list
Transform a list
Find elements within a list
Reusing lambda expressions
Code examples
The good parts of Java 8 Support for lambda expressions
New time API
Collections API has been given a major overhaul
Functional interfaces
Default methods
Concurrency is much easier!!
…and the bad parts of
Java 8 whyjavasucks.com
Still no operator overloading
Haven’t seen this many changes since Java 5
Lambda expressions
The code is not directly readable without interpretation
A developer that reads the code will need to know the
code reasonably well
Java 101 It’s OO
Everything is an object
except primitives!!
Every class creates instances of objects
you can’t pass a method as an argument or return a method body for an instance
Haskell and Lisp are two good examples of functional programming languages.
JavaScript is consider to be one as well. (well actually both)
OO v/s Functional Datafields are treated as
objects manipulated through
pre-defined methods
Terms such as..
Inheritance
Polymorphism
Encapsulation
Computation as the
evaluation of functions
Imperative v/s Declarative Telling the machine “HOW”
to do something
Result = what you wanted to
happen will happen
Telling the machine “WHAT”
you would like to happen
Result = let the computer
figure it out
But Isaac we love imperative
programming! We are VERY used to describing the “HOW”
Love controlling everything
Makes us feel good and comfortable
Even makes us feel powerful
Don’t like to leave things up to “magic”
With the power of today’s languages and compilers, we no longer need to worry about such trivial things
…and this is where lambda expressions come into play…
Syntax of a lambda
expression Basic syntax
(parameters) --> (expression)
(parameters) --> {statements;}
Examples…
() --> 5
takes no value and returns 5
x --> 2 * x
takes a number and returns the result of doubling it
(int x, int y) --> x + y
takes two integers and returns their sum
(String s) --> System.out.print(s)
takes a string and prints it to the console without returning anything
Where did .forEach() come
from? As of Java 8, the Iterable interface now contains the
.forEach()
Performs the given action for each element of the
Iterable until all elements have been processed
Accepts a Consumer type parameter
Is a Higher Order function
What is a Consumer??? Represents an operation that accepts only one
argument and returns void
..is a functional interface
…and due to this, it can be used as the assignment target
for a lambda expression or method reference
Example…
Consumer<String> consumer =
(String string) --> (System.out.println(string));
Functional interface?? Java already contains the concept of SAM interfaces
Single Abstract Method interfaces
Most likely have used them by creating an anonymous inner class
Examples of use are...Runnable, ActionListener, and Comparator
So a Functional Interface is the same as a SAM interface
Java 8 is saying that they can now be represented using lambda expressions.
Not going to get into the how’s and why’s of Functional Interfaces
Such as..
Not required to use the @FunctionalInterface
Helps the compiler
Be kind to your follower developers and use the annotation
What is a Higher Order
function? Means that it is possible to PASS to a method both
values and functions and in the same way the method
itself can return either a value or or a function
Examples…
public void doSomething(String name, Function some
Function) {…..}
public Function createAnAction(Integer someInteger)
{….do some logic and return back a function..}
What is .stream() ?? Not to be confused with InputStream or OutputStream
In functional programming terms, it’s a “monad”
Represents a sequence of elements and provides
different kinds of operations to perform computations
upon those elements
Stream operations are either intermediate or terminal
What is a Predicate ? A statement that may be true or false depending on the
values of its variables
Functional interface
Where would you use a Predicate?
Anywhere where you need to evaluate a condition on a
group/collection that results in a boolean
So in summary… Learn to use lambda expressions
Let Java do all the work
Program more declaratively
Don’t try to customize everything
Unique time for programming
Java is no longer just OO
Sources Lambda Expressions for Dummies
Functional Programming in Java: Harnessing the power of Java 8 Lambda Expresssions
Java 8 API docs
Imperative v/s Declarative programming
Programming Paradigms Wiki
Start using Java Lambda Expressions
Java Method Reference
…more sources Oracle documentation over lambdas
Java 8 and Predicates