SlideShare a Scribd company logo
Functional Aspects of Java 8
Shahadat Hossain Chowdhury (Jobaer)
Technical Project Manager
Cefalo AS
Goal of my talk
Things I will talk about
Lamda
Streams
Optional
Lambda
Lambda structure
Parameters -> Body
(Apple a1, Apple a2) ->
a1.getWeight().compareTo(a2.getWeight());
We can use lambda to
parameterize behaviors
A refactoring example
public class Apple {
private String color;
private Integer weight;
public Apple(color, weight) …
public String getColor() …
public void setColor(color) …
public Integer getWeight() …
public void setWeight(weight) …
}
There was an apple store ...
We want to filter all green apples
filterGreenApples(List<Apple> inventory){
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory) {
if("green".equals(apple.getColor())) {
result.add(apple);
}
}
return result;
}
greenApples = filterGreenApples(inventory);
Attempt 1
Filter the “red” ones too!
List<Apple> filterByColor(inventory,color){
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory) {
if(color.equals(apple.getColor())) {
result.add(apple);
}
}
return result;
}
filterByColor(inventory, "green");
filterByColor(inventory, "red");
Attempt 2
And filter also by “weight”
Attempt 3
List<Apple> filterApples(
inventory, color, weight, flag) {
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory) {
if((flag && color.equals(apple.getColor()))
|| (!flag && apple.getWeight() >= weight)) {
result.add(apple);
}
}
return result;
}
filterApples(inventory, "green", 0, true);
filterApples(inventory, "red", 0, true);
filterApples(inventory, "", 150, false);
public interface ApplePredicate {
boolean test(Apple apple);
}
class GreenPredicate implements … {
@Override
public boolean test(Apple apple) {
return "green".equals(apple.getColor());
}
}
Attempt 4
HeavyPredicate implements ApplePredicate {
public boolean test(Apple apple) {
return apple.getWeight() >= 150;
}
}
RedAndHeavyPredicate implements … {
public boolean test(Apple apple) {
return "red".equals(apple.getColor())
&& apple.getWeight() >= 150;
}
}
Attempt 4 ...
filterApples(
inventory, ApplePredicate predicate) {
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory) {
if(predicate.test(apple)) {
result.add(apple);
}
}
return result;
}
filterApples(inventory, new GreenPredicate());
filterApples(inventory, new AppleHeavyPredicate());
filterApples(inventory, new RedAndHeavyPredicate());
Attempt 4 ...
List<Apple> inventory = getInventory();
greenApples = filterApples(
inventory, new ApplePredicate() {
public boolean test(Apple apple) {
return "red".equals(apple.getColor());
}
});
Attempt 5 … anon class
greenApples = filterApples(
inventory,
apple -> "red".equals(apple.getColor()));
heavyApples = filterApples(
inventory,
apple -> apple.getWeight() >= 150);
Attempt 6 .. Power of lambda
public interface Predicate<T> {
boolean test(T t);
}
List<T> filter(List<T> list,
Predicate<T> p) {
List<T> result = new ArrayList<>();
for (T e : list) {
if (p.test(e)) {
result.add(e);
}
}
return result;
}
Final attempt .. more generic
greenApples = filter(
inventory, (Apple a) ->
"green".equals(a.getColor()));
heavyApples = filter(
inventory,
(Apple a) -> a.getWeight() >= 150);
numbers = Arrays.asList(1,2,3,4,5,6,7,8);
evenNumbers = filter(
numbers, (Integer i) -> i % 2 == 0);
Final attempt .. more generic
Lambda in a nutshell
● Anonymous
● Function
● Passed around
● Concise
Misc. lambda use
Event handling
Label l = new Label("...");
Button b = new Button();
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed (
ActionEvent e) {
l.setText("Sending...");
}
});
Without lambda
b.addActionListener(
(ActionEvent e) -> l.setText("Sending.. ")
);
Using lambda
Implement runnables
Thread regular = new Thread( new Runnable()
{
@Override
public void run() {
System.out.println("From runnable");
}
});
Traditional way
Thread lambda = new Thread(
() -> System.out.println("From lambda"));
With lambda
Using in comparators
List<Apple> inventory = getInventory();
inventory.sort(new AppleComparator());
class AppleComparator implements
Comparator<Apple> {
@Override
public int compare(Apple a1, Apple a2) {
return a1.getWeight().compareTo(
a2.getWeight());
}
}
Sort with named comparator
List<Apple> inventory = getInventory();
inventory.sort(new Comparator<Apple>() {
@Override
public int compare(Apple a1, Apple a2) {
return a1.getWeight().compareTo(
a2.getWeight());
}
});
Sort with Anon. comparator
List<Apple> inventory = getInventory();
inventory.sort(
(a1, a2) ->
a1.getWeight().compareTo(
a2.getWeight()));
Sort with Lambda
List<Apple> inventory = getInventory();
inventory.sort(
comparing(Apple::getWeight));
Sort with method reference
Composing lambdas/functions
List<Apple> inventory = getInventory();
inventory.sort(comparing(Apple::getWeight);
inventory.sort(
comparing(Apple::getWeight).reversed());
inventory.sort(
comparing(Apple::getColor).
thenComparing(Apple::getWeight));
Compose comparators
List<Apple> inventory = getInventory();
Predicate<Apple> redApple =
apple -> "red".equals(apple.getColor());
Predicate<Apple> notRedApple =
redApple.negate();
inventory.stream().filter(notRedApple);
Compose Predicates …
Compose Predicates
Predicate<Apple> redAndHeavy =
redApple.and(apple ->
apple.getWeight() > 150);
redApple
.and(a -> a.getWeight() > 150)
.or(a -> "green".equals(a.getColor()));
Compose Functions
Function<...> inc = a -> a + 1;
Function<...> dub = a -> a * 2;
Function<..> incThenDub = inc.andThen(dub);
incThenDub.apply(9) // returns 20
Function<..> incOfDub = inc.compose(dub);
incOfDub.apply(9) // returns 19
But where it can be used?
class Letter {
static String addHeader(String text) {
return "From Bob: " + text;
}
static String addFooter(String text) {
return text + "nKind regards";
}
static String checkSpelling(String text) {
return
text.replaceAll("labda", "lambda");
}
}
Compose Functions
Function<..> addHeader = Letter::addHeader;
Function<..> pipeline1 =
addHeader
.andThen(Letter::checkSpelling)
.andThen(Letter::addFooter);
Function<..> pipeline2 =
addHeader
.andThen(Letter::addFooter);
There’s more about lambda, but I
am not covering here
Streams
Let’s start with an example
Domain class
public class Dish {
Dish(String name, int calories) ..
public String getName() ...
public int getCalories() ...
...
}
Get the low calorie dish names,
sorted by calorie
Java 7 approach ...
//First we get all the low cal dishes
List<Dish> lowCals = new ArrayList<Dish>();
for (Dish dish : menu) {
if(dish.getCalories() < 400){
lowCals.add(dish);
}
}
Java 7 approach ...
// Sort the dishes according to calorie
Collections.sort(lowCals, new
Comparator<Dish>() {
public int compare(Dish d1, Dish d2) {
return Integer.compare
(d1.getCalories(), d2.getCalories());
}
});
// And now get the names as result
List<String> lowCalNames = new ArrayList<>();
for (Dish lowCal : lowCals) {
lowCalNames.add(lowCal.getName());
}
Come to Java 8
List<String> newLowCalNames =
menu.stream()
.filter(d -> d.getCalories() < 400)
.sorted(comparing(Dish::getCalories))
.map(Dish::getName)
.collect(toList());
Difference with Collection
Collection Streams
Traversable only once.
Traversing again will throw
IllegalStageException!
External iterator vs. internal
iterator
//External iteration
List<String> names = new ArrayList<>();
for(Dish d : menu) {
names.add(d.getName());
}
//Internal iteration
List<String> names = menu.stream()
.map(Dish::getName).collect(toList());
Stream operations
● Intermediate Operations
○ filter, map, limit, sorted,
distinct, skip etc.
● Terminal operations
○ forEach, count, collect, reduce
Reducing ...
Integer sum(List<Integer> numbers ) {
int result = 0;
for (Integer number : numbers) {
result = result + number;
}
return result;
}
Integer product(List<Integer> numbers) {
int result = 1;
for(Integer num : numbers) {
result = result * num;
}
return result;
}
Reducing
Integer sumByReduce =
ints.stream()
.reduce(0, (a, b) -> a + b);
Integer prodByReduce =
ints.stream()
.reduce(1, (a, b) -> a * b);
Streams api let’s you write code
that is ..
● Declarative
● Composable
● Parallelizable
I just touched the surface.
Have a look a the full api
http://docs.oracle.com/javase/8/docs/api/java/util/stream/package
-summary.html
Optional<T>
Tony Hoare
He developed the Quicksort
Algorithm
He also invented another thing …
Null reference
What’s wrong with this method?
String getCarInsuranceName(Person person){
return person.getCar()
.getInsurance()
.getName();
}
It may fail spectacularly with a
NullPointerException!
So we need to avoid nulls at any
cost!
Avoiding nulls - attempt 1
String getCarInsuranceName(Person person) {
if(person != null){
Car car = person.getCar();
if(car != null) {
Insurance ins = car.getInsurance();
if(ins != null){
return ins.getName();
}
}
}
return “Unknown”;
}
Avoiding nulls - early exit
String getCarInsuranceName(Person person) {
if(person == null) {
return “Unknown”;
}
Car car = person.getCar();
if(car == null){
return “Unknown”;
}
Insurance ins = car.getInsurance();
if(ins == null){
return “Unknown”;
}
return ins.getName();
}
Problems with Null
● Source of error
● Bloats your code
● Breaks java philosophy
● Hole in the type system
Meet Optional
Optional<Car> optCar = Optional.empty();
Optional<Car> optCar = Optional.of(car);
Optional<Car> optCar =
Optional.ofNullable(car);
Working with Optional
Optional<Insurance> optInsurance =
Optional.ofNullable(insurance);
Optional<String> name =
optInsurance.map(Insurance::getName);
String value = name.orElse(“Unknown”);
Let’s refactor our previous code
Car getCar() .. // previous
Optional<Car> getCar() … // now
Insurance getInsurace() … // previous
Optional<Insurance> getInsurance() … // now
and so on ..
And then we may try ...
Optional<Person>
optPerson = Optional.ofNullable(person);
Optional<String> name = optPerson
.map(Person::getCar)
.map(Car::getInsurance)
.map(Insurance::getName);
But that doesn’t work ...
Optional<Person> optPerson =
Optional.of(person);
optPerson.map(Person::getCar)
//returns Optional<Optional<Car>>
Mighty ‘flatMap’ to the rescue
This is what ‘map’ does
person.flatMap(Person::getCar)
will return a Optional<Car>
instead of
Optional<Optional<Car>>
This is what ‘flatMap’ does
Power of Optional and flatMap
String getCarInsuranceName(
Optional<Person> person) {
return
person.flatMap(Person::getCar)
.flatMap(Car::getInsurance)
.map(Insurance::getName)
.orElse(“Unknown”);
}
There are some other features
which isn’t covered here
So in general functional
programming lets us write code
that is ...
Power of Optional and flatMap
String getCarInsuranceName(
Optional<Person> person) {
return
person.flatMap(Person::getCar)
.flatMap(Car::getInsurance)
.map(Insurance::getName)
.orElse(“Unknown”);
}
Before I finish ...
Keep learning!
Learn some other functional
languages
Some books
Some MOOCs
https://www.edx.org/course/introduction-functional-programming-delftx-fp101x
https://www.coursera.org/course/progfun
https://www.coursera.org/course/reactive
Don’t rush!
Please don’t!
Thank you very much!
Example code on github.
https://github.com/JobaerChowdhury/fpinjava
Follow me on twitter
@JobaerChowdhury
Find me on linkedin
http://bd.linkedin.com/in/jobaer
Questions?

More Related Content

What's hot

"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu
"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu
"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu
ThinkOpen
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
Ganesh Samarthyam
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional Programming
Jordan Parmer
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
Mario Fusco
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
daewon jeong
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
Mario Fusco
 
Future vs. Monix Task
Future vs. Monix TaskFuture vs. Monix Task
Future vs. Monix Task
Hermann Hueck
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
John De Goes
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)
Bikram Thapa
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forward
Mario Fusco
 
Introduction to Swift programming language.
Introduction to Swift programming language.Introduction to Swift programming language.
Introduction to Swift programming language.
Icalia Labs
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
Mario Fusco
 
SeaJUG March 2004 - Groovy
SeaJUG March 2004 - GroovySeaJUG March 2004 - Groovy
SeaJUG March 2004 - Groovy
Ted Leung
 
The Ring programming language version 1.5.3 book - Part 34 of 184
The Ring programming language version 1.5.3 book - Part 34 of 184The Ring programming language version 1.5.3 book - Part 34 of 184
The Ring programming language version 1.5.3 book - Part 34 of 184
Mahmoud Samir Fayed
 
Python cheatsheet Indian Servers
Python cheatsheet Indian ServersPython cheatsheet Indian Servers
Python cheatsheet Indian Servers
Indian Servers
 
Ramda lets write declarative js
Ramda   lets write declarative jsRamda   lets write declarative js
Ramda lets write declarative js
Pivorak MeetUp
 
The Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect SystemThe Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect System
John De Goes
 
Post-Free: Life After Free Monads
Post-Free: Life After Free MonadsPost-Free: Life After Free Monads
Post-Free: Life After Free Monads
John De Goes
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
Sandeep Kr. Singh
 

What's hot (20)

"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu
"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu
"Java 8, Lambda e la programmazione funzionale" by Theodor Dumitrescu
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional Programming
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Future vs. Monix Task
Future vs. Monix TaskFuture vs. Monix Task
Future vs. Monix Task
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forward
 
Introduction to Swift programming language.
Introduction to Swift programming language.Introduction to Swift programming language.
Introduction to Swift programming language.
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
 
SeaJUG March 2004 - Groovy
SeaJUG March 2004 - GroovySeaJUG March 2004 - Groovy
SeaJUG March 2004 - Groovy
 
The Ring programming language version 1.5.3 book - Part 34 of 184
The Ring programming language version 1.5.3 book - Part 34 of 184The Ring programming language version 1.5.3 book - Part 34 of 184
The Ring programming language version 1.5.3 book - Part 34 of 184
 
Python cheatsheet Indian Servers
Python cheatsheet Indian ServersPython cheatsheet Indian Servers
Python cheatsheet Indian Servers
 
Ramda lets write declarative js
Ramda   lets write declarative jsRamda   lets write declarative js
Ramda lets write declarative js
 
The Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect SystemThe Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect System
 
Post-Free: Life After Free Monads
Post-Free: Life After Free MonadsPost-Free: Life After Free Monads
Post-Free: Life After Free Monads
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 

Similar to Functional aspects of java 8

Java8
Java8Java8
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
Knoldus Inc.
 
Java 8: more readable and flexible code
Java 8: more readable and flexible codeJava 8: more readable and flexible code
Java 8: more readable and flexible code
WeAreEsynergy
 
Wien15 java8
Wien15 java8Wien15 java8
Wien15 java8
Jaanus Pöial
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
J On The Beach
 
Lazy java
Lazy javaLazy java
Lazy java
Mario Fusco
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
Nicola Pedot
 
Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018
Codemotion
 
Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)
Calvin Cheng
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
Skills Matter
 
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
Akaks
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
Sanjoy Kumar Roy
 
Java 8 by example!
Java 8 by example!Java 8 by example!
Java 8 by example!
Mark Harrison
 
JDK8 : parallel programming made (too ?) easy
JDK8 : parallel programming made (too ?) easyJDK8 : parallel programming made (too ?) easy
JDK8 : parallel programming made (too ?) easy
José Paumard
 
Scala @ TomTom
Scala @ TomTomScala @ TomTom
Scala @ TomTom
Eric Bowman
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
Maýur Chourasiya
 
Functional programming
Functional programmingFunctional programming
Functional programming
Christian Hujer
 
Fp java8
Fp java8Fp java8
Fp java8
Yanai Franchi
 
Scala introduction
Scala introductionScala introduction
Scala introduction
Alf Kristian Støyle
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 

Similar to Functional aspects of java 8 (20)

Java8
Java8Java8
Java8
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
 
Java 8: more readable and flexible code
Java 8: more readable and flexible codeJava 8: more readable and flexible code
Java 8: more readable and flexible code
 
Wien15 java8
Wien15 java8Wien15 java8
Wien15 java8
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
Lazy java
Lazy javaLazy java
Lazy java
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018
 
Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
 
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
Java 8 by example!
Java 8 by example!Java 8 by example!
Java 8 by example!
 
JDK8 : parallel programming made (too ?) easy
JDK8 : parallel programming made (too ?) easyJDK8 : parallel programming made (too ?) easy
JDK8 : parallel programming made (too ?) easy
 
Scala @ TomTom
Scala @ TomTomScala @ TomTom
Scala @ TomTom
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Fp java8
Fp java8Fp java8
Fp java8
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
 

Recently uploaded

Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 

Recently uploaded (20)

Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 

Functional aspects of java 8

  • 1. Functional Aspects of Java 8 Shahadat Hossain Chowdhury (Jobaer) Technical Project Manager Cefalo AS
  • 2. Goal of my talk
  • 3. Things I will talk about Lamda Streams Optional
  • 5. Lambda structure Parameters -> Body (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());
  • 6. We can use lambda to parameterize behaviors
  • 8. public class Apple { private String color; private Integer weight; public Apple(color, weight) … public String getColor() … public void setColor(color) … public Integer getWeight() … public void setWeight(weight) … } There was an apple store ...
  • 9. We want to filter all green apples
  • 10. filterGreenApples(List<Apple> inventory){ List<Apple> result = new ArrayList<>(); for(Apple apple : inventory) { if("green".equals(apple.getColor())) { result.add(apple); } } return result; } greenApples = filterGreenApples(inventory); Attempt 1
  • 11. Filter the “red” ones too!
  • 12. List<Apple> filterByColor(inventory,color){ List<Apple> result = new ArrayList<>(); for(Apple apple : inventory) { if(color.equals(apple.getColor())) { result.add(apple); } } return result; } filterByColor(inventory, "green"); filterByColor(inventory, "red"); Attempt 2
  • 13. And filter also by “weight”
  • 14. Attempt 3 List<Apple> filterApples( inventory, color, weight, flag) { List<Apple> result = new ArrayList<>(); for(Apple apple : inventory) { if((flag && color.equals(apple.getColor())) || (!flag && apple.getWeight() >= weight)) { result.add(apple); } } return result; } filterApples(inventory, "green", 0, true); filterApples(inventory, "red", 0, true); filterApples(inventory, "", 150, false);
  • 15. public interface ApplePredicate { boolean test(Apple apple); } class GreenPredicate implements … { @Override public boolean test(Apple apple) { return "green".equals(apple.getColor()); } } Attempt 4
  • 16. HeavyPredicate implements ApplePredicate { public boolean test(Apple apple) { return apple.getWeight() >= 150; } } RedAndHeavyPredicate implements … { public boolean test(Apple apple) { return "red".equals(apple.getColor()) && apple.getWeight() >= 150; } } Attempt 4 ...
  • 17. filterApples( inventory, ApplePredicate predicate) { List<Apple> result = new ArrayList<>(); for(Apple apple : inventory) { if(predicate.test(apple)) { result.add(apple); } } return result; } filterApples(inventory, new GreenPredicate()); filterApples(inventory, new AppleHeavyPredicate()); filterApples(inventory, new RedAndHeavyPredicate()); Attempt 4 ...
  • 18. List<Apple> inventory = getInventory(); greenApples = filterApples( inventory, new ApplePredicate() { public boolean test(Apple apple) { return "red".equals(apple.getColor()); } }); Attempt 5 … anon class
  • 19. greenApples = filterApples( inventory, apple -> "red".equals(apple.getColor())); heavyApples = filterApples( inventory, apple -> apple.getWeight() >= 150); Attempt 6 .. Power of lambda
  • 20. public interface Predicate<T> { boolean test(T t); } List<T> filter(List<T> list, Predicate<T> p) { List<T> result = new ArrayList<>(); for (T e : list) { if (p.test(e)) { result.add(e); } } return result; } Final attempt .. more generic
  • 21. greenApples = filter( inventory, (Apple a) -> "green".equals(a.getColor())); heavyApples = filter( inventory, (Apple a) -> a.getWeight() >= 150); numbers = Arrays.asList(1,2,3,4,5,6,7,8); evenNumbers = filter( numbers, (Integer i) -> i % 2 == 0); Final attempt .. more generic
  • 22. Lambda in a nutshell ● Anonymous ● Function ● Passed around ● Concise
  • 25. Label l = new Label("..."); Button b = new Button(); b.addActionListener(new ActionListener() { @Override public void actionPerformed ( ActionEvent e) { l.setText("Sending..."); } }); Without lambda
  • 26. b.addActionListener( (ActionEvent e) -> l.setText("Sending.. ") ); Using lambda
  • 28. Thread regular = new Thread( new Runnable() { @Override public void run() { System.out.println("From runnable"); } }); Traditional way
  • 29. Thread lambda = new Thread( () -> System.out.println("From lambda")); With lambda
  • 31. List<Apple> inventory = getInventory(); inventory.sort(new AppleComparator()); class AppleComparator implements Comparator<Apple> { @Override public int compare(Apple a1, Apple a2) { return a1.getWeight().compareTo( a2.getWeight()); } } Sort with named comparator
  • 32. List<Apple> inventory = getInventory(); inventory.sort(new Comparator<Apple>() { @Override public int compare(Apple a1, Apple a2) { return a1.getWeight().compareTo( a2.getWeight()); } }); Sort with Anon. comparator
  • 33. List<Apple> inventory = getInventory(); inventory.sort( (a1, a2) -> a1.getWeight().compareTo( a2.getWeight())); Sort with Lambda
  • 34. List<Apple> inventory = getInventory(); inventory.sort( comparing(Apple::getWeight)); Sort with method reference
  • 36. List<Apple> inventory = getInventory(); inventory.sort(comparing(Apple::getWeight); inventory.sort( comparing(Apple::getWeight).reversed()); inventory.sort( comparing(Apple::getColor). thenComparing(Apple::getWeight)); Compose comparators
  • 37. List<Apple> inventory = getInventory(); Predicate<Apple> redApple = apple -> "red".equals(apple.getColor()); Predicate<Apple> notRedApple = redApple.negate(); inventory.stream().filter(notRedApple); Compose Predicates …
  • 38. Compose Predicates Predicate<Apple> redAndHeavy = redApple.and(apple -> apple.getWeight() > 150); redApple .and(a -> a.getWeight() > 150) .or(a -> "green".equals(a.getColor()));
  • 39. Compose Functions Function<...> inc = a -> a + 1; Function<...> dub = a -> a * 2; Function<..> incThenDub = inc.andThen(dub); incThenDub.apply(9) // returns 20 Function<..> incOfDub = inc.compose(dub); incOfDub.apply(9) // returns 19
  • 40. But where it can be used? class Letter { static String addHeader(String text) { return "From Bob: " + text; } static String addFooter(String text) { return text + "nKind regards"; } static String checkSpelling(String text) { return text.replaceAll("labda", "lambda"); } }
  • 41. Compose Functions Function<..> addHeader = Letter::addHeader; Function<..> pipeline1 = addHeader .andThen(Letter::checkSpelling) .andThen(Letter::addFooter); Function<..> pipeline2 = addHeader .andThen(Letter::addFooter);
  • 42. There’s more about lambda, but I am not covering here
  • 44. Let’s start with an example
  • 45. Domain class public class Dish { Dish(String name, int calories) .. public String getName() ... public int getCalories() ... ... }
  • 46. Get the low calorie dish names, sorted by calorie
  • 47. Java 7 approach ... //First we get all the low cal dishes List<Dish> lowCals = new ArrayList<Dish>(); for (Dish dish : menu) { if(dish.getCalories() < 400){ lowCals.add(dish); } }
  • 48. Java 7 approach ... // Sort the dishes according to calorie Collections.sort(lowCals, new Comparator<Dish>() { public int compare(Dish d1, Dish d2) { return Integer.compare (d1.getCalories(), d2.getCalories()); } }); // And now get the names as result List<String> lowCalNames = new ArrayList<>(); for (Dish lowCal : lowCals) { lowCalNames.add(lowCal.getName()); }
  • 49. Come to Java 8 List<String> newLowCalNames = menu.stream() .filter(d -> d.getCalories() < 400) .sorted(comparing(Dish::getCalories)) .map(Dish::getName) .collect(toList());
  • 52. Traversable only once. Traversing again will throw IllegalStageException!
  • 53. External iterator vs. internal iterator //External iteration List<String> names = new ArrayList<>(); for(Dish d : menu) { names.add(d.getName()); } //Internal iteration List<String> names = menu.stream() .map(Dish::getName).collect(toList());
  • 54. Stream operations ● Intermediate Operations ○ filter, map, limit, sorted, distinct, skip etc. ● Terminal operations ○ forEach, count, collect, reduce
  • 55. Reducing ... Integer sum(List<Integer> numbers ) { int result = 0; for (Integer number : numbers) { result = result + number; } return result; } Integer product(List<Integer> numbers) { int result = 1; for(Integer num : numbers) { result = result * num; } return result; }
  • 56. Reducing Integer sumByReduce = ints.stream() .reduce(0, (a, b) -> a + b); Integer prodByReduce = ints.stream() .reduce(1, (a, b) -> a * b);
  • 57. Streams api let’s you write code that is .. ● Declarative ● Composable ● Parallelizable
  • 58. I just touched the surface. Have a look a the full api http://docs.oracle.com/javase/8/docs/api/java/util/stream/package -summary.html
  • 61. He developed the Quicksort Algorithm
  • 62. He also invented another thing …
  • 64. What’s wrong with this method? String getCarInsuranceName(Person person){ return person.getCar() .getInsurance() .getName(); }
  • 65. It may fail spectacularly with a NullPointerException!
  • 66. So we need to avoid nulls at any cost!
  • 67. Avoiding nulls - attempt 1 String getCarInsuranceName(Person person) { if(person != null){ Car car = person.getCar(); if(car != null) { Insurance ins = car.getInsurance(); if(ins != null){ return ins.getName(); } } } return “Unknown”; }
  • 68. Avoiding nulls - early exit String getCarInsuranceName(Person person) { if(person == null) { return “Unknown”; } Car car = person.getCar(); if(car == null){ return “Unknown”; } Insurance ins = car.getInsurance(); if(ins == null){ return “Unknown”; } return ins.getName(); }
  • 69. Problems with Null ● Source of error ● Bloats your code ● Breaks java philosophy ● Hole in the type system
  • 70. Meet Optional Optional<Car> optCar = Optional.empty(); Optional<Car> optCar = Optional.of(car); Optional<Car> optCar = Optional.ofNullable(car);
  • 71. Working with Optional Optional<Insurance> optInsurance = Optional.ofNullable(insurance); Optional<String> name = optInsurance.map(Insurance::getName); String value = name.orElse(“Unknown”);
  • 72. Let’s refactor our previous code Car getCar() .. // previous Optional<Car> getCar() … // now Insurance getInsurace() … // previous Optional<Insurance> getInsurance() … // now and so on ..
  • 73. And then we may try ... Optional<Person> optPerson = Optional.ofNullable(person); Optional<String> name = optPerson .map(Person::getCar) .map(Car::getInsurance) .map(Insurance::getName);
  • 74. But that doesn’t work ... Optional<Person> optPerson = Optional.of(person); optPerson.map(Person::getCar) //returns Optional<Optional<Car>>
  • 76. This is what ‘map’ does
  • 77. person.flatMap(Person::getCar) will return a Optional<Car> instead of Optional<Optional<Car>> This is what ‘flatMap’ does
  • 78. Power of Optional and flatMap String getCarInsuranceName( Optional<Person> person) { return person.flatMap(Person::getCar) .flatMap(Car::getInsurance) .map(Insurance::getName) .orElse(“Unknown”); }
  • 79. There are some other features which isn’t covered here
  • 80. So in general functional programming lets us write code that is ...
  • 81. Power of Optional and flatMap String getCarInsuranceName( Optional<Person> person) { return person.flatMap(Person::getCar) .flatMap(Car::getInsurance) .map(Insurance::getName) .orElse(“Unknown”); }
  • 84. Learn some other functional languages
  • 86.
  • 87.
  • 94. Thank you very much! Example code on github. https://github.com/JobaerChowdhury/fpinjava Follow me on twitter @JobaerChowdhury Find me on linkedin http://bd.linkedin.com/in/jobaer