SlideShare a Scribd company logo
Functional
Programming
Lambda expressions in Java 8
Functional Programming
• What is Functional Programming and Predicate
(example from predicates logics of the first order).
• It’s properties and peculiarities (no states and
passing the control, one big function, no cycles)
Functional Programming -
Definition
• Функційне програмування є способом створення
, ,програм в яких єдиною дією є виклик функції
єдиним способом розбиття програми є
створення нового імені функції та задання для
,цього імені виразу що обчислює значення
,функції а єдиним правилом композиції є
.оператор суперпозиції функцій Жодних комірок
' , , , ,пам яті операторів присвоєння циклів ні тим
, - .більше блок схем чи передачі управління
Determine if number is
prime ( )простое
1. Write in good old Java
2. Write in Lambdas
IsPrime (imperative)
/*
* Imperative - how we do it
* is mutable
*/
private static boolean isPrimeImperative(final int number) {
for (int i = 2; i < number; i++) {
if (number % i == 0)
return false;
}
return true;
}
IsPrime (declarative)
/*
* Declarative - what we do
* is immutable
*/
private static boolean isPrimeDeclarative(final int number) {
return IntStream.range(2, number).noneMatch(i -> number % i
== 0);
}
Get first doubled number
greater than 3
Imperative style
Declarative style using list Arrays.asList(1, 2, 5, 4, 6, 5, 4, 3, 8)
Make inner methods
Add sout to methods to see amount of steps
Make method excluding findFirst().get() and explain lazy
initialisation.
Show other ways to address to methods inside of others (through
lightbulb)
Students try on their own do similar with IntStream
Get first doubled number
greater than 3 Declarative
private static int
getEvenDoubledGreaterThanList(List<Integer> values,
final int number) {
return values.stream()
.filter(i -> i > number)
.filter(i -> i % 2 == 0)
.map(i -> 2 * i)
.findFirst().get();
Get first doubled number greater
than 3 Declarative with functions
private static int getEvenDoubledGreaterThanListSample(List<Integer> values, final int number) {
return getEvenDoubleGreaterThan3Stream(values)
.findFirst().get();
}
private static Stream<Integer> getEvenDoubleGreaterThan3Stream(List<Integer> values) {
return values.stream()
.filter(FunctionalProgramming::isGreaterThan3 )
.filter(FunctionalProgramming::isEven)
.map(FunctionalProgramming::doubleNumber);
}
private static Function<Integer, Integer> multiplyBy2() {
return i -> 2 * i;
}
private static int doubleNumber(int number){
System.out.printf("nLet's double -> " + number);
return number * 2;
}
private static Predicate<Integer> isEven() {
return i -> i % 2 == 0;
}
private static boolean isEven(Integer number) {
System.out.printf("nIs even -> " + number);
return number % 2 == 0;
}
private static boolean isGreaterThan3(Integer number) {
System.out.printf("nIs greater than 3 -> " + number);
return number > 3;
}
Get first doubled number
greater than 3 with IntStream
private static int getEvenDoubledGreaterThan(final int
number) {
return IntStream.range(number + 1, 100)
.filter(i -> i % 2 == 0)
.map(i -> 2 * i)
.findFirst()
.getAsInt();
}
Interface with one method
1. Create an interface with one boolean method.
2. Write realisation of the method: static
OneMethodInterface cond1 = e -> e > 2;
3. Use it in filter
4. Students create similar stuff but with integer
method: public int condition1 (int element);
Interface with one method
Implementation
public class Interfaces {
// static OneMethodInterface cond1 = e -> e > 2;
static OneMethodInterface cond2 = e -> e % 2;
public static void main(String[] args) {
call(cond2);
}
private static void call(OneMethodInterface interf) {
// Stream.of(1, 3,2, 6,3,8).filter(i -> interf.condition(i)).forEach(i -> System.out.println(i));
// Stream.of(1, 3,2, 6,3,8).filter(interf::condition).forEach(i -> System.out.println(i));
Stream.of(1, 3,2, 6,3,8).map(interf::condition1).forEach(System.out::println);
}
public interface OneMethodInterface {
// public boolean condition (int element);
public int condition1 (int element);
}
}
Dependency Injection Using
Strategy Pattern
Strategy Pattern in Old Java
1. Task: count total of List of integers
2. Add if statement to pick which values to sum
using interface.
3. Create a Selector interface with one boolean
method pick(int) and add it to total parameters.
4. Write realisation for the interface.
Strategy Pattern in Old Java
Implementation
public class InterfacesDI1 {
public static void main(String[] args) {
System.out.println(totalValues(Arrays.asList(1, 5, 3, 2, 8), new EvenSelector()));
}
public static int totalValues(List<Integer> values, Selector selector){
int sum = 0;
for (Integer value : values) {
if(selector.pick(value))
sum +=value;
}
return sum;
}
interface Selector{
public boolean pick(int element);
}
static class EvenSelector implements Selector{
@Override
public boolean pick(int element) {
return element % 2 == 0;
}
}
}
Strategy Pattern in lambdas
1. Task: count total of List of integers
2. Change interface for Predicate
3. Write realisation of it’s test method instead of pick
and use it
4. Rewrite total method into lambdas
5. Delete redundant
Strategy Pattern in lambdas
Implementation
public class InterfacesDI2 {
public static void main(String[] args) {
System.out.println(totalValues(Arrays.asList(1, 5, 3, 2, 8), e -> true));
System.out.println(totalValues(Arrays.asList(1, 5, 3, 2, 8), e -> e % 2 == 0));
}
public static int totalValues(List<Integer> values, Predicate<Integer> selector) {
return values.stream().filter(selector).reduce(0, Math::addExact);
}
}
TDD + flatMap
1. Task: using TDD write Developer object
with name and list of languages.
2. Create several developer objects
3. Make a team
4. Collect all the languages they know
TDD + flatMap
Implementationpublic class FlatMapTest {
public static void main(String[] args) {
FlatMapTest flatMapTest = new FlatMapTest();
flatMapTest.flatMap();
}
@Test
public void flatMap() {
List<Developer> team = new ArrayList<>();
Developer polyglot = new Developer("esoteric");
polyglot.add("clojure");
polyglot.add("scala");
polyglot.add("groovy");
polyglot.add("go");
Developer busy = new Developer("pragmatic");
busy.add("java");
busy.add("javascript");
team.add(polyglot);
team.add(busy);
List<String> teamLanguages = team.stream().
map(d -> d.getLanguages()).
flatMap(l -> l.stream()).
collect(Collectors.toList());
System.out.println(teamLanguages);
}
private class Developer {
String name;
List<String> langs = new LinkedList<>();
public Developer(String name) {
this.name = name;
}
private void add(String lang) {
langs.add(lang);
}
private List<String> getLanguages() {
return this.langs;
}
}
}
Instance of
public static void tryInstanceOf() {
List<String> strings = Stream.of(1, 2.03, "Petit France",
3).filter(String.class::isInstance).map(String.class::cast).collect(Collectors.toList());
System.out.println("This is list " + strings);
String string = Stream.of(1, 2.03, "Petit France",
3).filter(String.class::isInstance).map(String.class::cast).collect(Collectors.joining(","));
System.out.println("This is string " + string);
Stream.of(1, 2.03, "Petit France",
3).filter(String.class::isInstance).map(String.class::cast).forEach(e -> System.out.println("This
is string in lambdas for each " + e));
Stream.of(1, 2.03, "Petit France",
3).filter(Integer.class::isInstance).map(Integer.class::cast).forEach(System.out::println);
Stream.of(1, 2.03, "Petit France", 3).filter(e -> e instanceof Double).map(e -> (Double)
e).forEach(System.out::println);
}
Home work
• Copy-paste method in the task on this link in your
code
http://stackoverflow.com/questions/25439277/lambdas-mu
• Implement it using TDD
• Rewrite it into lambdas
• Check yourself
Maps + Sort
1. Create a map and feel it with values
2. Try to sort it by values
3. Add writing to LinkedHashMap
collect(Collectors.toMap(Map.Entry::getKey,
Map.Entry::getValue, (v1, v2) -> v1,
LinkedHashMap::new)));
4. Look
https://docs.oracle.com/javase/tutorial/collections/interfaces/m
Maps + Sort Example
public static void main(String[] args) {
Map<Integer, String> map = new LinkedHashMap<>();
map.put(2, "2");
map.put(3, "3");
map.put(1, "1");
map.entrySet().stream().forEach(System.out::println);
// map.entrySet().stream().forEach(e -> System.out.println(e));
System.out.println(map);
System.out.println((Object) map.entrySet().stream().sorted((o1, o2) ->
o2.getValue().compareTo(o1.getValue())).
collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v2,
LinkedHashMap::new)));
}
Homework for sorting
• Sort the cities and apartments in them in order of
having the nearest free apartment starting form the
given date.
Supplier or Lazy initialisation
+ empty parameter
1. Write method <R> R use(Supplier<R> mapper)
2. Use mapper.get in it
3. Write methods that use the method in the way
use(() -> {…return …});
4. Try return nothing
5. Return different parameters in «use» method and
in the method that uses «use».
Consumer and other types
of functions.
• Takes an argument and returns nothing
Types of functions in Java 8
Immutable and Mutable
1. Don't provide "setter" methods — methods that modify fields or objects referred to by
fields.
2. Make all fields final and private.
3. Don't allow subclasses to override methods. The simplest way to do this is to
declare the class as final. A more sophisticated approach is to make the constructor
private and construct instances in factory methods.
4. If the instance fields include references to mutable objects, don't allow those objects
to be changed:
- Don’t provide methods that modify the mutable objects.
-Don’t share references to the mutable objects. Never store references to external,
mutable objects passed to the constructor; if necessary, create copies, and store
references to the copies. Similarly, create copies of your internal mutable objects
when necessary to avoid returning the originals in your methods.

More Related Content

PPTX
Is java8 a true functional programming language
SQLI
 
PPTX
Is java8a truefunctionallanguage
Samir Chekkal
 
PDF
Java 8 lambda expressions
Logan Chien
 
PDF
Java 8 Lambda Expressions
Scott Leberknight
 
PDF
Java 8 Stream API. A different way to process collections.
David Gómez García
 
PDF
FP in Java - Project Lambda and beyond
Mario Fusco
 
DOC
CS2309 JAVA LAB MANUAL
Lavanya Arunachalam A
 
PDF
OOP and FP - Become a Better Programmer
Mario Fusco
 
Is java8 a true functional programming language
SQLI
 
Is java8a truefunctionallanguage
Samir Chekkal
 
Java 8 lambda expressions
Logan Chien
 
Java 8 Lambda Expressions
Scott Leberknight
 
Java 8 Stream API. A different way to process collections.
David Gómez García
 
FP in Java - Project Lambda and beyond
Mario Fusco
 
CS2309 JAVA LAB MANUAL
Lavanya Arunachalam A
 
OOP and FP - Become a Better Programmer
Mario Fusco
 

What's hot (20)

PDF
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
PPS
Class method
kamal kotecha
 
PPT
Google collections api an introduction
gosain20
 
PDF
Java programming lab manual
sameer farooq
 
DOCX
Java programming lab_manual_by_rohit_jaiswar
ROHIT JAISWAR
 
PDF
Java Lab Manual
Naveen Sagayaselvaraj
 
PDF
From object oriented to functional domain modeling
Mario Fusco
 
PPT
Java 8 Streams
Manvendra Singh
 
PPTX
Python functions
ToniyaP1
 
PPTX
Chapter i(introduction to java)
Chhom Karath
 
PPTX
Lecture02 class -_templatev2
Hariz Mustafa
 
PDF
The Ring programming language version 1.5.1 book - Part 30 of 180
Mahmoud Samir Fayed
 
PPTX
Lecture05 operator overloading-and_exception_handling
Hariz Mustafa
 
PPTX
C# Generics
Rohit Vipin Mathews
 
PDF
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 
PDF
Harnessing the Power of Java 8 Streams
IndicThreads
 
PPTX
Generics in .NET, C++ and Java
Sasha Goldshtein
 
PPT
Extractors & Implicit conversions
Knoldus Inc.
 
PPT
Generics
Simon Smith
 
PDF
Parallel streams in java 8
David Gómez García
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
Class method
kamal kotecha
 
Google collections api an introduction
gosain20
 
Java programming lab manual
sameer farooq
 
Java programming lab_manual_by_rohit_jaiswar
ROHIT JAISWAR
 
Java Lab Manual
Naveen Sagayaselvaraj
 
From object oriented to functional domain modeling
Mario Fusco
 
Java 8 Streams
Manvendra Singh
 
Python functions
ToniyaP1
 
Chapter i(introduction to java)
Chhom Karath
 
Lecture02 class -_templatev2
Hariz Mustafa
 
The Ring programming language version 1.5.1 book - Part 30 of 180
Mahmoud Samir Fayed
 
Lecture05 operator overloading-and_exception_handling
Hariz Mustafa
 
C# Generics
Rohit Vipin Mathews
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 
Harnessing the Power of Java 8 Streams
IndicThreads
 
Generics in .NET, C++ and Java
Sasha Goldshtein
 
Extractors & Implicit conversions
Knoldus Inc.
 
Generics
Simon Smith
 
Parallel streams in java 8
David Gómez García
 
Ad

Viewers also liked (17)

PPTX
Power
8.patricia Trama
 
PPTX
Presentación1
hidalgojhon
 
PDF
ΚΑΤΟΨΗ-ΑΣΑΝΣΕΡ
Giorgos Gagos
 
PDF
Cool patern of circles colored
inspirato
 
PDF
Praktetk Microsoft Word
M. Dedy Deva Sanjaya Hata Babelin
 
PDF
Everett_CC
Robert S. Miller
 
PDF
Dr.SSR-171016
somasekhar bondalapati
 
PPTX
Introdução à programação - Aula 5
Clara Ferreira
 
ODP
Fotos josep i paula
jesus benito pellicer
 
PPT
Vedic mathematics engleski 14
mraduljain31
 
PPTX
the power to inspire
Constantina Cămașă
 
PDF
Regulamento um conto_que_contas_concurso201617
Ebimontargil Pte
 
ODT
Resumen t 5 españa, fin del antiguo régimen
Pablo Díaz
 
PDF
LifeSaverTech_Infographic New
Jeff Gregory
 
PDF
Employee Live Cycle JAX 2016
Stephan Schmidt
 
DOCX
Teorias de la investigacion
Pacc1996
 
Presentación1
hidalgojhon
 
ΚΑΤΟΨΗ-ΑΣΑΝΣΕΡ
Giorgos Gagos
 
Cool patern of circles colored
inspirato
 
Praktetk Microsoft Word
M. Dedy Deva Sanjaya Hata Babelin
 
Everett_CC
Robert S. Miller
 
Dr.SSR-171016
somasekhar bondalapati
 
Introdução à programação - Aula 5
Clara Ferreira
 
Fotos josep i paula
jesus benito pellicer
 
Vedic mathematics engleski 14
mraduljain31
 
the power to inspire
Constantina Cămașă
 
Regulamento um conto_que_contas_concurso201617
Ebimontargil Pte
 
Resumen t 5 españa, fin del antiguo régimen
Pablo Díaz
 
LifeSaverTech_Infographic New
Jeff Gregory
 
Employee Live Cycle JAX 2016
Stephan Schmidt
 
Teorias de la investigacion
Pacc1996
 
Ad

Similar to Functional Programming (20)

PDF
Functional aspects of java 8
Jobaer Chowdhury
 
PPTX
FUNctional Programming in Java 8
Richard Walker
 
PDF
Functional Programming 101 for Java 7 Developers
Jayaram Sankaranarayanan
 
PDF
Functional Java 8 in everyday life
Andrea Iacono
 
PPTX
Project Lambda: Evolution of Java
Can Pekdemir
 
PDF
Java 8 - functional features
Rafal Rybacki
 
PPTX
Java8lambda
Isuru Samaraweera
 
PPTX
Lambdas, Collections Framework, Stream API
Prabu U
 
PPTX
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
PDF
Java 8 - A step closer to Parallelism
jbugkorea
 
PDF
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
PPTX
Functional programming with Java 8
LivePerson
 
PDF
Java 8 by example!
Mark Harrison
 
PPTX
Week-1..................................
kmjanani05
 
PDF
Java 8 Workshop
Mario Fusco
 
PDF
Java SE 8 library design
Stephen Colebourne
 
PPTX
Exploring Streams and Lambdas in Java8
Isuru Samaraweera
 
PDF
Fp java8
Yanai Franchi
 
PPTX
Functional programming
Lhouceine OUHAMZA
 
PPT
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
Functional aspects of java 8
Jobaer Chowdhury
 
FUNctional Programming in Java 8
Richard Walker
 
Functional Programming 101 for Java 7 Developers
Jayaram Sankaranarayanan
 
Functional Java 8 in everyday life
Andrea Iacono
 
Project Lambda: Evolution of Java
Can Pekdemir
 
Java 8 - functional features
Rafal Rybacki
 
Java8lambda
Isuru Samaraweera
 
Lambdas, Collections Framework, Stream API
Prabu U
 
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
Java 8 - A step closer to Parallelism
jbugkorea
 
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
Functional programming with Java 8
LivePerson
 
Java 8 by example!
Mark Harrison
 
Week-1..................................
kmjanani05
 
Java 8 Workshop
Mario Fusco
 
Java SE 8 library design
Stephen Colebourne
 
Exploring Streams and Lambdas in Java8
Isuru Samaraweera
 
Fp java8
Yanai Franchi
 
Functional programming
Lhouceine OUHAMZA
 
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 

More from Olexandra Dmytrenko (9)

PPTX
R2DBC - Good Enough for Production?
Olexandra Dmytrenko
 
PPTX
Playing programming with kids and bb-8
Olexandra Dmytrenko
 
PDF
Playing Programming with Kids and BB-8
Olexandra Dmytrenko
 
PDF
Память в Java. Garbage Collector
Olexandra Dmytrenko
 
PDF
Рекурсия. Поиск
Olexandra Dmytrenko
 
PDF
Собеседование на позицию Java Developer
Olexandra Dmytrenko
 
PPTX
HTML Tables
Olexandra Dmytrenko
 
PPT
Discovering Lambdas (Speech)
Olexandra Dmytrenko
 
R2DBC - Good Enough for Production?
Olexandra Dmytrenko
 
Playing programming with kids and bb-8
Olexandra Dmytrenko
 
Playing Programming with Kids and BB-8
Olexandra Dmytrenko
 
Память в Java. Garbage Collector
Olexandra Dmytrenko
 
Рекурсия. Поиск
Olexandra Dmytrenko
 
Собеседование на позицию Java Developer
Olexandra Dmytrenko
 
HTML Tables
Olexandra Dmytrenko
 
Discovering Lambdas (Speech)
Olexandra Dmytrenko
 

Recently uploaded (20)

PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
PPTX
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
PDF
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PPTX
TestNG for Java Testing and Automation testing
ssuser0213cb
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
TestNG for Java Testing and Automation testing
ssuser0213cb
 
Exploring AI Agents in Process Industries
amoreira6
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 

Functional Programming

  • 2. Functional Programming • What is Functional Programming and Predicate (example from predicates logics of the first order). • It’s properties and peculiarities (no states and passing the control, one big function, no cycles)
  • 3. Functional Programming - Definition • Функційне програмування є способом створення , ,програм в яких єдиною дією є виклик функції єдиним способом розбиття програми є створення нового імені функції та задання для ,цього імені виразу що обчислює значення ,функції а єдиним правилом композиції є .оператор суперпозиції функцій Жодних комірок ' , , , ,пам яті операторів присвоєння циклів ні тим , - .більше блок схем чи передачі управління
  • 4. Determine if number is prime ( )простое 1. Write in good old Java 2. Write in Lambdas
  • 5. IsPrime (imperative) /* * Imperative - how we do it * is mutable */ private static boolean isPrimeImperative(final int number) { for (int i = 2; i < number; i++) { if (number % i == 0) return false; } return true; }
  • 6. IsPrime (declarative) /* * Declarative - what we do * is immutable */ private static boolean isPrimeDeclarative(final int number) { return IntStream.range(2, number).noneMatch(i -> number % i == 0); }
  • 7. Get first doubled number greater than 3 Imperative style Declarative style using list Arrays.asList(1, 2, 5, 4, 6, 5, 4, 3, 8) Make inner methods Add sout to methods to see amount of steps Make method excluding findFirst().get() and explain lazy initialisation. Show other ways to address to methods inside of others (through lightbulb) Students try on their own do similar with IntStream
  • 8. Get first doubled number greater than 3 Declarative private static int getEvenDoubledGreaterThanList(List<Integer> values, final int number) { return values.stream() .filter(i -> i > number) .filter(i -> i % 2 == 0) .map(i -> 2 * i) .findFirst().get();
  • 9. Get first doubled number greater than 3 Declarative with functions private static int getEvenDoubledGreaterThanListSample(List<Integer> values, final int number) { return getEvenDoubleGreaterThan3Stream(values) .findFirst().get(); } private static Stream<Integer> getEvenDoubleGreaterThan3Stream(List<Integer> values) { return values.stream() .filter(FunctionalProgramming::isGreaterThan3 ) .filter(FunctionalProgramming::isEven) .map(FunctionalProgramming::doubleNumber); } private static Function<Integer, Integer> multiplyBy2() { return i -> 2 * i; } private static int doubleNumber(int number){ System.out.printf("nLet's double -> " + number); return number * 2; } private static Predicate<Integer> isEven() { return i -> i % 2 == 0; } private static boolean isEven(Integer number) { System.out.printf("nIs even -> " + number); return number % 2 == 0; } private static boolean isGreaterThan3(Integer number) { System.out.printf("nIs greater than 3 -> " + number); return number > 3; }
  • 10. Get first doubled number greater than 3 with IntStream private static int getEvenDoubledGreaterThan(final int number) { return IntStream.range(number + 1, 100) .filter(i -> i % 2 == 0) .map(i -> 2 * i) .findFirst() .getAsInt(); }
  • 11. Interface with one method 1. Create an interface with one boolean method. 2. Write realisation of the method: static OneMethodInterface cond1 = e -> e > 2; 3. Use it in filter 4. Students create similar stuff but with integer method: public int condition1 (int element);
  • 12. Interface with one method Implementation public class Interfaces { // static OneMethodInterface cond1 = e -> e > 2; static OneMethodInterface cond2 = e -> e % 2; public static void main(String[] args) { call(cond2); } private static void call(OneMethodInterface interf) { // Stream.of(1, 3,2, 6,3,8).filter(i -> interf.condition(i)).forEach(i -> System.out.println(i)); // Stream.of(1, 3,2, 6,3,8).filter(interf::condition).forEach(i -> System.out.println(i)); Stream.of(1, 3,2, 6,3,8).map(interf::condition1).forEach(System.out::println); } public interface OneMethodInterface { // public boolean condition (int element); public int condition1 (int element); } }
  • 14. Strategy Pattern in Old Java 1. Task: count total of List of integers 2. Add if statement to pick which values to sum using interface. 3. Create a Selector interface with one boolean method pick(int) and add it to total parameters. 4. Write realisation for the interface.
  • 15. Strategy Pattern in Old Java Implementation public class InterfacesDI1 { public static void main(String[] args) { System.out.println(totalValues(Arrays.asList(1, 5, 3, 2, 8), new EvenSelector())); } public static int totalValues(List<Integer> values, Selector selector){ int sum = 0; for (Integer value : values) { if(selector.pick(value)) sum +=value; } return sum; } interface Selector{ public boolean pick(int element); } static class EvenSelector implements Selector{ @Override public boolean pick(int element) { return element % 2 == 0; } } }
  • 16. Strategy Pattern in lambdas 1. Task: count total of List of integers 2. Change interface for Predicate 3. Write realisation of it’s test method instead of pick and use it 4. Rewrite total method into lambdas 5. Delete redundant
  • 17. Strategy Pattern in lambdas Implementation public class InterfacesDI2 { public static void main(String[] args) { System.out.println(totalValues(Arrays.asList(1, 5, 3, 2, 8), e -> true)); System.out.println(totalValues(Arrays.asList(1, 5, 3, 2, 8), e -> e % 2 == 0)); } public static int totalValues(List<Integer> values, Predicate<Integer> selector) { return values.stream().filter(selector).reduce(0, Math::addExact); } }
  • 18. TDD + flatMap 1. Task: using TDD write Developer object with name and list of languages. 2. Create several developer objects 3. Make a team 4. Collect all the languages they know
  • 19. TDD + flatMap Implementationpublic class FlatMapTest { public static void main(String[] args) { FlatMapTest flatMapTest = new FlatMapTest(); flatMapTest.flatMap(); } @Test public void flatMap() { List<Developer> team = new ArrayList<>(); Developer polyglot = new Developer("esoteric"); polyglot.add("clojure"); polyglot.add("scala"); polyglot.add("groovy"); polyglot.add("go"); Developer busy = new Developer("pragmatic"); busy.add("java"); busy.add("javascript"); team.add(polyglot); team.add(busy); List<String> teamLanguages = team.stream(). map(d -> d.getLanguages()). flatMap(l -> l.stream()). collect(Collectors.toList()); System.out.println(teamLanguages); } private class Developer { String name; List<String> langs = new LinkedList<>(); public Developer(String name) { this.name = name; } private void add(String lang) { langs.add(lang); } private List<String> getLanguages() { return this.langs; } } }
  • 20. Instance of public static void tryInstanceOf() { List<String> strings = Stream.of(1, 2.03, "Petit France", 3).filter(String.class::isInstance).map(String.class::cast).collect(Collectors.toList()); System.out.println("This is list " + strings); String string = Stream.of(1, 2.03, "Petit France", 3).filter(String.class::isInstance).map(String.class::cast).collect(Collectors.joining(",")); System.out.println("This is string " + string); Stream.of(1, 2.03, "Petit France", 3).filter(String.class::isInstance).map(String.class::cast).forEach(e -> System.out.println("This is string in lambdas for each " + e)); Stream.of(1, 2.03, "Petit France", 3).filter(Integer.class::isInstance).map(Integer.class::cast).forEach(System.out::println); Stream.of(1, 2.03, "Petit France", 3).filter(e -> e instanceof Double).map(e -> (Double) e).forEach(System.out::println); }
  • 21. Home work • Copy-paste method in the task on this link in your code http://stackoverflow.com/questions/25439277/lambdas-mu • Implement it using TDD • Rewrite it into lambdas • Check yourself
  • 22. Maps + Sort 1. Create a map and feel it with values 2. Try to sort it by values 3. Add writing to LinkedHashMap collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new))); 4. Look https://docs.oracle.com/javase/tutorial/collections/interfaces/m
  • 23. Maps + Sort Example public static void main(String[] args) { Map<Integer, String> map = new LinkedHashMap<>(); map.put(2, "2"); map.put(3, "3"); map.put(1, "1"); map.entrySet().stream().forEach(System.out::println); // map.entrySet().stream().forEach(e -> System.out.println(e)); System.out.println(map); System.out.println((Object) map.entrySet().stream().sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue())). collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v2, LinkedHashMap::new))); }
  • 24. Homework for sorting • Sort the cities and apartments in them in order of having the nearest free apartment starting form the given date.
  • 25. Supplier or Lazy initialisation + empty parameter 1. Write method <R> R use(Supplier<R> mapper) 2. Use mapper.get in it 3. Write methods that use the method in the way use(() -> {…return …}); 4. Try return nothing 5. Return different parameters in «use» method and in the method that uses «use».
  • 26. Consumer and other types of functions. • Takes an argument and returns nothing Types of functions in Java 8
  • 27. Immutable and Mutable 1. Don't provide "setter" methods — methods that modify fields or objects referred to by fields. 2. Make all fields final and private. 3. Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods. 4. If the instance fields include references to mutable objects, don't allow those objects to be changed: - Don’t provide methods that modify the mutable objects. -Don’t share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.