SlideShare una empresa de Scribd logo
1 de 30
FUNctional Programming
in Java 8
An introduction to functional programming, lambdas, and streams
Richard Walker
September 2016
Outline
 Java is changing
 Paradigm shift
 What is functional programming?
 Anonymous functions (Lambdas)
 Streams
 Filter
 Map
 Reduce
 Examples
Java is evolving and so should we
 “It is not the strongest of the species that survives, nor the most intelligent
that survives. It is the one that is most adaptable to change” – Charles Darwin
 Java 8 allows for cleaner and more concise code
 Takes advantage of multicore architectures without synchronized
Collections.sort(inventory, new Comparator <Apple>() {
public int compare (Apple a1, Apple a2) {
return a1.getWeight().compareTo(a2.getWeight());
}
});
inventory.sort(comparing(Apple::getWeight)); Java 8
We’re still learning how to code
Procedural
Object-
Oriented
Functional
C, Fortran,
Perl
Java, C++,
Python
Lisp, Scala, Clojure,
Java 8*
https://twitter.com/jongold/status/757262853388677120
https://twitter.com/iamdevloper
What is functional programming?
 Declarative Programming Paradigm
 What you want done instead of how it gets done
 Accomplish tasks by composing small functions
 No side effects
 No mutation = easier debugging
 Allows for safe parallelization
Quick Quiz
Imperative Way
int result = 0;
for(int e: numbers) {
if(e % 2 == 0) {
result += e * 2;
}
}
Functional Way I
numbers.stream()
.filter(e -> e % 2 == 0)
.mapToInt(e -> e * 2)
.sum();
Functional Way II
numbers.stream()
.filter(Numbers::evenNumber)
.mapToInt(Numbers::doubleNumber)
.sum();
Lambdas
 Anonymous functions
 like an anonymous inner class
 Functional Interfaces
 An interface with one abstract method (NEW!)*
 1st-class citizens in Java 8
Format:
(parameters) -> (expression)
Examples:
(int a, int b) -> a * b
(a, b) -> a * b
Functional Interfaces in Java
 Oracle provided a bunch of them fresh out of the box!
Predicate<T> : T -> Boolean
Function<T, R> : T -> R
https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
Lambda Example
// Java 7
List<Person> people = loadPeople();
Collections.sort(people, new Comparator<Person>()
{
@Override
public int compare(Person p1, Person p2) {
return p1.name.compareTo(p2.name);
}
});
Lambda Example
// Java 7
List<Person> people = loadPeople();
Collections.sort(people, new Comparator<Person>()
{
@Override
public int compare(Person p1, Person p2) {
return p1.name.compareTo(p2.name);
}
});
Lambda Example
// Java 8
List<Person> people = loadPeople();
Collections.sort(people,
(Person p1, Person p2)
p1.name.compareTo(p2.name);
);
Lambda Example
// Java 8
List<Person> people = loadPeople();
Collections.sort(people,
(Person p1, Person p2) -> p1.name.compareTo(p2.name);
);
Lambda Example
// Java 8
List<Person> people = loadPeople();
Collections.sort(people,
(Person p1, Person p2) -> p1.name.compareTo(p2.name);
);
Lambda Example
// Java 8
List<Person> people = loadPeople();
Collections.sort(people,
(p1, p2) -> p1.name.compareTo(p2.name);
);
Lambda Example
// Java 8
List<Person> people = loadPeople();
people.sort((p1, p2) -> p1.name.compareTo(p2.name));
• Less code
• Easier to read
• Less chance for screw ups
We’re using the new sort API added to java.util.List in Java 8 – instead of the old Collections.sort API.
Streams
“The gateway drug of Java 8” - Venkat Subramaniam
What are Streams?
 Allows for Collection manipulation in a
Declarative way
 Functional
 Allows us to abstract away from iteration
 Less boilerplate code
 Allows for composable code
 Greater Flexibility
 Allows for easy (free) parallelization
 Better Performance
Filtering (Extracts)
// Filtering unique elements
List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
numbers.stream()
.filter(i -> i % 2 == 0)
.distinct()
.forEach(System.out::println);
Mapping (Transforms)
// Map
List<String> words = Arrays.asList("Hello", "World");
List<Integer> wordLengths = words.stream()
.map(String::length)
.collect(toList());
System.out.println(wordLengths);
Reducing (Folds/Massages)
List<Integer> numbers = Arrays.asList(3,4,5,1,2);
int sum = numbers.stream()
.reduce(0, (a, b) -> a + b);
System.out.println(sum);
int sum2 = numbers.stream()
.reduce(0, Integer::sum);
System.out.println(sum2);
Filtering (Extracts) faster!
// Filtering unique elements
List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
numbers.parallelStream()
.filter(i -> i % 2 == 0)
.distinct()
.forEach(System.out::println);
Examples
How can I actually use this at work?
class Person {
Gender gender; String name;
public Gender getGender() { return gender; }
public String getName() { return name; }
}
enum Gender { MALE, FEMALE, OTHER }
List<String> names = new ArrayList<String>();
List<Person> people = …
people.stream()
.filter(p -> p.getGender() == Gender.FEMALE)
.map(Person::getName)
.map(String::toUpperCase)
.forEach(name -> names.add(name));
List of uppercase names of all the “FEMALE” people in
that list
Get the unique surnames in uppercase of the first
15 book authors that are 50 years old or older.
library.stream()
.map(book -> book.getAuthor())
.filter(author -> author.getAge() >= 50)
.map(Author::getSurname)
.map(String::toUpperCase)
.distinct()
.limit(15)
.collect(toList()));
Real-life examples
Reducing QA work one line at a time
QA Implementation Pt.1
public static PartnerInfo[] arrayCopy(final PartnerInfo... info) {
if (info == null) {
return null;
}
final PartnerInfo[] result = new PartnerInfo[info.length];
for (int i = 0; i < info.length; i++) {
result[i] = new PartnerInfo(info[i]);
}
return result;
}
286 chars, 9 lines
QA Implementation Pt.2
public static PartnerInfo[] arrayCopy(final PartnerInfo... info) {
if (info == null) {
return null;
}
// Create a new copy for each PartnerInfo in the list
return Arrays.stream(info)
.map(PartnerInfo::new)
.toArray(PartnerInfo[]::new);
}
194 chars, 7 lines
Differences
Imperative
 9 Lines
 286 Chars
Declarative
 7 Lines
 194 Chars
92 characters less
32.17% decrease!
Conclusion
 Paradigms are changing, and languages are evolving
 Java 8 has a host of new features:
 Lambdas
 Stream API
 Functional Code has the following benefits:
 Flexible
 Less Code*

Más contenido relacionado

La actualidad más candente

Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
Binoj T E
 
SWT Lecture Session 8 - Inference in jena
SWT Lecture Session 8 - Inference in jenaSWT Lecture Session 8 - Inference in jena
SWT Lecture Session 8 - Inference in jena
Mariano Rodriguez-Muro
 

La actualidad más candente (20)

collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
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
 
Introduction to java 8 stream api
Introduction to java 8 stream apiIntroduction to java 8 stream api
Introduction to java 8 stream api
 
JDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done Right
JDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done RightJDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done Right
JDD 2016 - Grzegorz Piwowarek - Davaslang - Functional Java Done Right
 
Array list
Array listArray list
Array list
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
 
Marcs (bio)perl course
Marcs (bio)perl courseMarcs (bio)perl course
Marcs (bio)perl course
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Presentation1
Presentation1Presentation1
Presentation1
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako,
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Free your lambdas
Free your lambdasFree your lambdas
Free your lambdas
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Hash set (java platform se 8 )
Hash set (java platform se 8 )Hash set (java platform se 8 )
Hash set (java platform se 8 )
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
SWT Lecture Session 8 - Inference in jena
SWT Lecture Session 8 - Inference in jenaSWT Lecture Session 8 - Inference in jena
SWT Lecture Session 8 - Inference in jena
 

Destacado

Destacado (16)

Java modules using project jigsaw@jdk 9
Java modules using project jigsaw@jdk 9Java modules using project jigsaw@jdk 9
Java modules using project jigsaw@jdk 9
 
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
 
Java 8 by example!
Java 8 by example!Java 8 by example!
Java 8 by example!
 
Workshop: Introduction to the Disruptor
Workshop: Introduction to the DisruptorWorkshop: Introduction to the Disruptor
Workshop: Introduction to the Disruptor
 
Java 8: the good parts!
Java 8: the good parts!Java 8: the good parts!
Java 8: the good parts!
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
Refactoring to Java 8 (Devoxx UK)
Refactoring to Java 8 (Devoxx UK)Refactoring to Java 8 (Devoxx UK)
Refactoring to Java 8 (Devoxx UK)
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Java 9 Functionality and Tooling
Java 9 Functionality and ToolingJava 9 Functionality and Tooling
Java 9 Functionality and Tooling
 
What's New in Java SE 9
What's New in Java SE 9What's New in Java SE 9
What's New in Java SE 9
 
Java 9 – The Ultimate Feature List
Java 9 – The Ultimate Feature ListJava 9 – The Ultimate Feature List
Java 9 – The Ultimate Feature List
 
55 New Features in Java SE 8
55 New Features in Java SE 855 New Features in Java SE 8
55 New Features in Java SE 8
 
Java 8 Stream API and RxJava Comparison
Java 8 Stream API and RxJava ComparisonJava 8 Stream API and RxJava Comparison
Java 8 Stream API and RxJava Comparison
 
Java 9 Modularity in Action
Java 9 Modularity in ActionJava 9 Modularity in Action
Java 9 Modularity in Action
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 

Similar a FUNctional Programming in Java 8

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
 
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
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8
James Brown
 

Similar a FUNctional Programming in Java 8 (20)

Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Java 8 new features or the ones you might actually use
Java 8 new features or the ones you might actually useJava 8 new features or the ones you might actually use
Java 8 new features or the ones you might actually use
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
 
JDK8 : parallel programming made (too ?) easy
JDK8 : parallel programming made (too ?) easyJDK8 : parallel programming made (too ?) easy
JDK8 : parallel programming made (too ?) easy
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
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
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 
Lambda Functions in Java 8
Lambda Functions in Java 8Lambda Functions in Java 8
Lambda Functions in Java 8
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Java 8
Java 8Java 8
Java 8
 
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
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
 
whats new in java 8
whats new in java 8 whats new in java 8
whats new in java 8
 
Wien15 java8
Wien15 java8Wien15 java8
Wien15 java8
 

FUNctional Programming in Java 8

  • 1. FUNctional Programming in Java 8 An introduction to functional programming, lambdas, and streams Richard Walker September 2016
  • 2. Outline  Java is changing  Paradigm shift  What is functional programming?  Anonymous functions (Lambdas)  Streams  Filter  Map  Reduce  Examples
  • 3. Java is evolving and so should we  “It is not the strongest of the species that survives, nor the most intelligent that survives. It is the one that is most adaptable to change” – Charles Darwin  Java 8 allows for cleaner and more concise code  Takes advantage of multicore architectures without synchronized Collections.sort(inventory, new Comparator <Apple>() { public int compare (Apple a1, Apple a2) { return a1.getWeight().compareTo(a2.getWeight()); } }); inventory.sort(comparing(Apple::getWeight)); Java 8
  • 4. We’re still learning how to code Procedural Object- Oriented Functional C, Fortran, Perl Java, C++, Python Lisp, Scala, Clojure, Java 8*
  • 6. What is functional programming?  Declarative Programming Paradigm  What you want done instead of how it gets done  Accomplish tasks by composing small functions  No side effects  No mutation = easier debugging  Allows for safe parallelization
  • 7. Quick Quiz Imperative Way int result = 0; for(int e: numbers) { if(e % 2 == 0) { result += e * 2; } } Functional Way I numbers.stream() .filter(e -> e % 2 == 0) .mapToInt(e -> e * 2) .sum(); Functional Way II numbers.stream() .filter(Numbers::evenNumber) .mapToInt(Numbers::doubleNumber) .sum();
  • 8. Lambdas  Anonymous functions  like an anonymous inner class  Functional Interfaces  An interface with one abstract method (NEW!)*  1st-class citizens in Java 8 Format: (parameters) -> (expression) Examples: (int a, int b) -> a * b (a, b) -> a * b
  • 9. Functional Interfaces in Java  Oracle provided a bunch of them fresh out of the box! Predicate<T> : T -> Boolean Function<T, R> : T -> R https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
  • 10. Lambda Example // Java 7 List<Person> people = loadPeople(); Collections.sort(people, new Comparator<Person>() { @Override public int compare(Person p1, Person p2) { return p1.name.compareTo(p2.name); } });
  • 11. Lambda Example // Java 7 List<Person> people = loadPeople(); Collections.sort(people, new Comparator<Person>() { @Override public int compare(Person p1, Person p2) { return p1.name.compareTo(p2.name); } });
  • 12. Lambda Example // Java 8 List<Person> people = loadPeople(); Collections.sort(people, (Person p1, Person p2) p1.name.compareTo(p2.name); );
  • 13. Lambda Example // Java 8 List<Person> people = loadPeople(); Collections.sort(people, (Person p1, Person p2) -> p1.name.compareTo(p2.name); );
  • 14. Lambda Example // Java 8 List<Person> people = loadPeople(); Collections.sort(people, (Person p1, Person p2) -> p1.name.compareTo(p2.name); );
  • 15. Lambda Example // Java 8 List<Person> people = loadPeople(); Collections.sort(people, (p1, p2) -> p1.name.compareTo(p2.name); );
  • 16. Lambda Example // Java 8 List<Person> people = loadPeople(); people.sort((p1, p2) -> p1.name.compareTo(p2.name)); • Less code • Easier to read • Less chance for screw ups We’re using the new sort API added to java.util.List in Java 8 – instead of the old Collections.sort API.
  • 17. Streams “The gateway drug of Java 8” - Venkat Subramaniam
  • 18. What are Streams?  Allows for Collection manipulation in a Declarative way  Functional  Allows us to abstract away from iteration  Less boilerplate code  Allows for composable code  Greater Flexibility  Allows for easy (free) parallelization  Better Performance
  • 19. Filtering (Extracts) // Filtering unique elements List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4); numbers.stream() .filter(i -> i % 2 == 0) .distinct() .forEach(System.out::println);
  • 20. Mapping (Transforms) // Map List<String> words = Arrays.asList("Hello", "World"); List<Integer> wordLengths = words.stream() .map(String::length) .collect(toList()); System.out.println(wordLengths);
  • 21. Reducing (Folds/Massages) List<Integer> numbers = Arrays.asList(3,4,5,1,2); int sum = numbers.stream() .reduce(0, (a, b) -> a + b); System.out.println(sum); int sum2 = numbers.stream() .reduce(0, Integer::sum); System.out.println(sum2);
  • 22. Filtering (Extracts) faster! // Filtering unique elements List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4); numbers.parallelStream() .filter(i -> i % 2 == 0) .distinct() .forEach(System.out::println);
  • 23. Examples How can I actually use this at work?
  • 24. class Person { Gender gender; String name; public Gender getGender() { return gender; } public String getName() { return name; } } enum Gender { MALE, FEMALE, OTHER } List<String> names = new ArrayList<String>(); List<Person> people = … people.stream() .filter(p -> p.getGender() == Gender.FEMALE) .map(Person::getName) .map(String::toUpperCase) .forEach(name -> names.add(name)); List of uppercase names of all the “FEMALE” people in that list
  • 25. Get the unique surnames in uppercase of the first 15 book authors that are 50 years old or older. library.stream() .map(book -> book.getAuthor()) .filter(author -> author.getAge() >= 50) .map(Author::getSurname) .map(String::toUpperCase) .distinct() .limit(15) .collect(toList()));
  • 26. Real-life examples Reducing QA work one line at a time
  • 27. QA Implementation Pt.1 public static PartnerInfo[] arrayCopy(final PartnerInfo... info) { if (info == null) { return null; } final PartnerInfo[] result = new PartnerInfo[info.length]; for (int i = 0; i < info.length; i++) { result[i] = new PartnerInfo(info[i]); } return result; } 286 chars, 9 lines
  • 28. QA Implementation Pt.2 public static PartnerInfo[] arrayCopy(final PartnerInfo... info) { if (info == null) { return null; } // Create a new copy for each PartnerInfo in the list return Arrays.stream(info) .map(PartnerInfo::new) .toArray(PartnerInfo[]::new); } 194 chars, 7 lines
  • 29. Differences Imperative  9 Lines  286 Chars Declarative  7 Lines  194 Chars 92 characters less 32.17% decrease!
  • 30. Conclusion  Paradigms are changing, and languages are evolving  Java 8 has a host of new features:  Lambdas  Stream API  Functional Code has the following benefits:  Flexible  Less Code*

Notas del editor

  1. Since the release of JDK 1.0 in 1996 Java has won a large following of supporters in the programming community, and it has evolved till it’s Java 8 release in 2014.