+ All Categories
Home > Software > Lambda Expressions in Java 8

Lambda Expressions in Java 8

Date post: 15-Jul-2015
Category:
Upload: icarter09
View: 117 times
Download: 7 times
Share this document with a friend
Popular Tags:
32
Lambda Expressions in Java 8 Presented by: Isaac Carter
Transcript

Lambda Expressions in

Java 8Presented by:

Isaac Carter

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

Is anyone currently using

Java 8?

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)

Java 8 can now do

functional

programming!!

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

But that’s really not the

best way to think about

Java 8 and lambdas…

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…

Some quick syntax before we

look at code…

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

Let’s look at some code

examples

** only look at examples using strings **

.forEach()

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

Consumer

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

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

Higher Order

Functions

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..}

.stream()

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

Predicate

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


Recommended