SlideShare a Scribd company logo
1 of 105
Download to read offline
Demian Neidetcher
FP in Java8
Who am I?
1995 20102000 2005
demian0311
linkedin.com/in/demian0311
neidetcher.com
demian0311@gmail.com
Questions
•Who is using Java?
•Who’s using a language with
FP features?
•Anyone using Java8?
Java and FP Agenda
•Intro & Context
•Non-FP features
•Lambdas
•Streams
Big Points
• Pragmatically speaking, FP helps you do efficient things with
collections, taking advantage of multi-core or distributed
systems
• Lambdas are small functions that you implement that extend
SAM Interfaces, likely the ones found in java.util.function.*
• Streams are where collections and lambdas come together, you
want to get familiar with java.util.stream.*
Intro
Background
• Java8 delayed for security improvements
and lambdas
• Brian Goetz is lead on lambdas
• Java8 released March 2014
• next release will feature Jigsaw
(modularity)
Lots of Cool Languages with Closures/ FP
But Still Lots of Java Work Out There
What makes a language functional?
• first class functions
• higher order functions
• recursion
• pure functions
FP and performance
• FP offers a way of working with
collections of data in a way that is easy
to make parallel
FP makes code more simple
• less ceremony
• fewer lines of code
FP makes code less error prone
• discourages mutable variables
• easier to understand
JVM languages with FP features
• Clojure
• Scala
• Groovy
• JRuby
• Jython
FP Languages
• 1959 Lisp
• 1975 ML, FP, Scheme
• 1978 Smalltalk
• 1986 Standard ML
• 1990 Haskell, Erlang
• 1999 XSLT
• 2000 OCaml
• 2003 Scala, XQuery
• 2005 F#
• 2007 Clojure
• 2011 C++11
Objections to FP
• learning curve
• not intuitive
• overly academic
Java8
Features
MetaSpace
No more permgen
• metaspace will grab native memory as
needed at runtime
• -XX:MaxMetaspaceSize=256m can
specify the upper bound
No more permgen
No more permgen
Optional
Optional
List
String String String String
Optional
List
String String String String
Optional
String
Using a Populated Optional
Using an Empty Optional
Giving Optional a Null
Creating an Optional with ofNullable()
Getting an Optional in Collections
Don’t call get() without checking
Equals and Hashcode for Optional
Using Option in your own beans
String Join
StringBuilder.append
StringJoiner
StringJoiner with forEach
String.join
Interface
Defender
Methods
Why Defender Methods
Iterator.forEach
Why Defender Methods
<<interface>>
Iterable
default forEach(Consumer<T>): void
<<interface>>
Collection
<<interface>>
List
<<class>>
ArrayList
Why Defender Methods
Our own Interface with Defender Method
Our own Interface with Defender Method
draw(): String
<<interface>>
Cowboy
<<class>>
Caballero
uses draw
from Interface
default
method
Disambiguating Default Methods
Disambiguating Default Methods
draw(): String
<<interface>>
Cowboy
draw(): String
<<interface>>
Circle
<<class>>
Circle
Cowboy
must
disambiguate
Classes Win
Classes Win
draw(): String
<<interface>>
Circle
<<abstract
class>>
DeckOfCards
<<class>>
CircleDeckOf
Cards
class wins
Interfaces vs Abstract Classes
feature interface abstract class
method definitions yes yes
method
implementations
yes with default yes
state no yes
have a constructor no yes
use synchronized
on methods
no yes
Lambdas
Lambdas on Their Own
Lambda Syntax
arguments
body
Lambdas from Functional Interfaces #0
Lambdas from Functional Interfaces #1
Lambdas from Functional Interfaces #2
Lambdas Remove Cruft
Single Abstract Method Interfaces
This annotation is optional,
just to keep you honest.
Single Abstract Method Interfaces
Runnable is a SAMI
Go into Runnable and show
how it is an interace with
only 1 abstract method
Comparator is a SAMI
New Functional Interfaces
• Function
• Predicate
• Consumer
• Supplier
All of these are found in
java.util.function
Function
Function
input
result
Predicate
Predicate
input
Boolean
Consumer
Consumer
input
Supplier
Supplier
result
UnaryOperator
SAMIs That Take Primitives
SAMIs That Return Primitives
Bi*
Lambdas with Streams #0
Lambdas with Streams #1
Lambdas with Streams #2
Lambdas with Streams #3
was this
Method References
Method References
Method References
Streams
Range from IntStream
From here on you
have a stream
Random Streams
This function
creates an
IntStream
Stream Pipeline
collection source
intermediate
intermediate
intermediate terminal
Stream Pipeline
ArrayList .stream()
.filter(…)
.map(…)
.sorted(…) .collect()
lamdas go
here
Stream Pipeline
source
intermediate
terminal
Streams Should be Familiar
demian@raven ~>ls /etc/init.d/ | grep mount |
sort -r > ~/stuff.txt
source
intermediate
terminal
Stream Method Taxonomy
Stream
Methods
Source
Intermediate
Terminal
filter
transform
sorted
reductions
collect
count
min
max
reduce
stream
map
Source
source
Intermediate: filter
Intermediate: map
Intermediate: sorted
Terminal: forEach
Terminal: reduce
Terminal: collect
Terminal: collect with multi args
Terminal: collect with groupingBy
Contrived Slow Function
Serial Stream
Parallel Stream
Parallel Streams
So is Java8 a Functional Language?
• recursion?
• pure functions?
• first class functions?
• higher order functions?
Recursion
Pure Functions
Functions that Take Another Function
Functions That Create Functions
So is Java8 a Functional Language?
Not at all a purely functional language.
What Next?
• Goetz talked about adding case classes
• I’d like matchers
• for comprehensions
!
def findChannelsForUser(id: Int): Option[List[String]] = {
for {
user <- userManager.findUserById(id)
account <- accountManager.findAccountForUser(user)
channels <- channelManager.findChannelsForAccount(account)
} yield (channels)
}
Big Points
• Pragmatically speaking, FP helps you do efficient things with
collections, taking advantage of multi-core or distributed
systems
• Lambdas are small functions that you implement that extend
SAM Interfaces, likely the ones found in java.util.function.*
• Streams are where collections and lambdas come together, you
want to get familiar with java.util.stream.*

More Related Content

What's hot

Intro to Functions Python
Intro to Functions PythonIntro to Functions Python
Intro to Functions Pythonprimeteacher32
 
Pharo Status ESUG 2014
Pharo Status ESUG 2014Pharo Status ESUG 2014
Pharo Status ESUG 2014Marcus Denker
 
Scala : language of the future
Scala : language of the futureScala : language of the future
Scala : language of the futureAnsviaLab
 
20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdasshinolajla
 
Functional Programming In Practice
Functional Programming In PracticeFunctional Programming In Practice
Functional Programming In PracticeMichiel Borkent
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigoujaxconf
 
CNIT 126 5: IDA Pro
CNIT 126 5: IDA ProCNIT 126 5: IDA Pro
CNIT 126 5: IDA ProSam Bowne
 
Functional Thinking - Programming with Lambdas in Java 8
Functional Thinking - Programming with Lambdas in Java 8Functional Thinking - Programming with Lambdas in Java 8
Functional Thinking - Programming with Lambdas in Java 8Ganesh Samarthyam
 
Erlang - Concurrent Language for Concurrent World
Erlang - Concurrent Language for Concurrent WorldErlang - Concurrent Language for Concurrent World
Erlang - Concurrent Language for Concurrent WorldZvi Avraham
 
Namespace in C++ Programming Language
Namespace in C++ Programming LanguageNamespace in C++ Programming Language
Namespace in C++ Programming LanguageHimanshu Choudhary
 
A brief tour of modern Java
A brief tour of modern JavaA brief tour of modern Java
A brief tour of modern JavaSina Madani
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To ScalaPeter Maas
 
KafNafParserPy: a python library for parsing/creating KAF and NAF files
KafNafParserPy: a python library for parsing/creating KAF and NAF filesKafNafParserPy: a python library for parsing/creating KAF and NAF files
KafNafParserPy: a python library for parsing/creating KAF and NAF filesRubén Izquierdo Beviá
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scalafanf42
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaJDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaSimon Ritter
 

What's hot (20)

Intro to Functions Python
Intro to Functions PythonIntro to Functions Python
Intro to Functions Python
 
Java 8 stream and c# 3.5
Java 8 stream and c# 3.5Java 8 stream and c# 3.5
Java 8 stream and c# 3.5
 
Pharo Status ESUG 2014
Pharo Status ESUG 2014Pharo Status ESUG 2014
Pharo Status ESUG 2014
 
Scala : language of the future
Scala : language of the futureScala : language of the future
Scala : language of the future
 
20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 
Functional Programming In Practice
Functional Programming In PracticeFunctional Programming In Practice
Functional Programming In Practice
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigou
 
CNIT 126 5: IDA Pro
CNIT 126 5: IDA ProCNIT 126 5: IDA Pro
CNIT 126 5: IDA Pro
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Functional Thinking - Programming with Lambdas in Java 8
Functional Thinking - Programming with Lambdas in Java 8Functional Thinking - Programming with Lambdas in Java 8
Functional Thinking - Programming with Lambdas in Java 8
 
Erlang - Concurrent Language for Concurrent World
Erlang - Concurrent Language for Concurrent WorldErlang - Concurrent Language for Concurrent World
Erlang - Concurrent Language for Concurrent World
 
Namespace in C++ Programming Language
Namespace in C++ Programming LanguageNamespace in C++ Programming Language
Namespace in C++ Programming Language
 
A brief tour of modern Java
A brief tour of modern JavaA brief tour of modern Java
A brief tour of modern Java
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To Scala
 
Lambdas and Laughs
Lambdas and LaughsLambdas and Laughs
Lambdas and Laughs
 
KafNafParserPy: a python library for parsing/creating KAF and NAF files
KafNafParserPy: a python library for parsing/creating KAF and NAF filesKafNafParserPy: a python library for parsing/creating KAF and NAF files
KafNafParserPy: a python library for parsing/creating KAF and NAF files
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaJDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
 
Java SE 8 library design
Java SE 8 library designJava SE 8 library design
Java SE 8 library design
 

Similar to 2014 java functional

Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsNewCircle Training
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New featuresSon Nguyen
 
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Simon Ritter
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsEmiel Paasschens
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streamsjessitron
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014Nayden Gochev
 
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 OverviewEclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 OverviewEclipse Day India
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usageAsmaShaikh478737
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhHarmeet Singh(Taara)
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New FeaturesNaveen Hegde
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8IndicThreads
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Simon Ritter
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterJAXLondon2014
 

Similar to 2014 java functional (20)

Java 8
Java 8Java 8
Java 8
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
 
14274730 (1).ppt
14274730 (1).ppt14274730 (1).ppt
14274730 (1).ppt
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New features
 
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3
 
Lambdas
LambdasLambdas
Lambdas
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
 
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 OverviewEclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 Overview
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usage
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New Features
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
 

2014 java functional