SlideShare a Scribd company logo
1 of 51
Project Lambda:
Evolution of Java
Can Pekdemir
“I would say, for most Java developers, it’s revolutionary. For
polyglot programmers, it’s evolutionary”.
Adam Bien - author of Real World Java EE Patterns
Agenda
• Programming paradigms
• Rise of functional programming
• Lambda expressions
• Functional Interface
• Stream API
• Other exciting Java 8 features
Programming Paradigms
• Main programming paradigms
• Imperative programming
• Functional programming
• Logic programming
• Object-Oriented Programming?
Imperative Programming
• Ideas of Von Neumann architecture digital hardware
technology
• First do this and next do that
• Concentrates on how
• Mutable variables
• Assignments, control structures if-then-else, loops, etc.
Imperative Programming
Total of discounted prices by then which are greater than 20.
BigDecimal totalOfDiscountedPrices = BigDecimal.ZERO;
for(BigDecimal price : prices) {
if(price.compareTo(BigDecimal.valueOf(20)) > 0)
totalOfDiscountedPrices =
totalOfDiscountedPrices.add(price.multiply(BigDecimal.valueOf(0.9)));
}
System.out.println("Total of discounted prices: “ +
totalOfDiscountedPrices);
Functional Programming
• Mathematics and the theory of functions
• John Backus:”Can programming be liberated from the von
Neumann Style?”
• Functions are first class citizens.
• Functions can be defined anywhere, inside other functions.
• Functions can be passed as argument.
• Typically avoids mutable state.
• What we want rather than how to do it.
Functional Programming
final BigDecimal totalOfDiscountedPrices = prices.stream()
.filter(price -> price.compareTo(BigDecimal.valueOf(20)) > 0)
.map(price -> price.multiply(BigDecimal.valueOf(0.9)))
.reduce(BigDecimal.ZERO, BigDecimal::add);
Declarative style: expressions over statements.
Executionin the Kingdomof Nouns
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("hi " + name);
}});
StateManager.getConsiderationSetter("Noun Oriented Thinking",
State.HARMFUL).run();
Steve Yegge
http://steve-yegge.blogspot.com.tr/2006/03/execution-in-
kingdom-of-nouns.html
The Difference
“OO makes code understandable by encapsulating moving parts.
FP makes code understandable by minimizing moving parts.”
Michael Feathers, author of “Working with legacy code”
Functional Programming Rises
• The rise of multicore CPUs and big data.
• Need for parallel programming with immutable data.
• Declarative programming
• Less clutter code
Java Evolution
• Lack of abstracting over behaviour, not just data
• Parallelism should be free, confusing thread api
• Need for lazy evaluation over data
Behaviour Abstraction
• First requirement: filter green apples
public static List<Apple> filterGreenApples(List<Apple> inventory){
List<Apple> result = new ArrayList<>();
for (Apple apple: inventory){
if ("green".equals(apple.getColor())) {
result.add(apple);
}
}
return result;
}
What if they want to according to “red”!
Behaviour Abstraction
• Abstracting over color
public static List<Apple> filterApplesByColour(List<Apple> inventory,
String color) {
List<Apple> result = new ArrayList<>();
for (Apple apple: inventory){
if ( apple.getColor().equals(color) ) {
result.add(apple);
}
}
return result;
}
- What if they want to filter according to weight!
Refactoring?
public static List<Apple> filterApples(List<Apple> inventory, String color, int
weight, boolean flag) {
List<Apple> result = new ArrayList<>();
for (Apple apple: inventory){
if ( (flag && apple.getColor().equals(color)) || (!flag &&
apple.getWeight() > weight) ){
result.add(apple);
}
}
return result;
}
List<Apple> greenApples = filterApples(inventory, "green", 0, true);
List<Apple> heavyApples = filterApples(inventory, "", 150, false);
Parameterize Behaviour
ApplePredicate encapsulates a strategy for selecting an apple
public interface ApplePredicate{
boolean test (Apple apple);
}
public class AppleWeightPredicate implements ApplePredicate{
public boolean test(Apple apple){
return apple.getWeight() > 150;
}
}
public class AppleColorPredicate implements ApplePredicate{
public boolean test(Apple apple){
return "green".equals(apple.getColor());
} }
Filtering by an Abstract Criterion
public static List<Apple> filterApples(List<Apple> inventory,
ApplePredicate p){
List<Apple> result = new ArrayList<>();
for(Apple apple: inventory){
if(p.test(apple)){
result.add(apple);
}
}
return result;
}
Filter with Anonymous Class
List<Apple> redApples = filterApples(inventory,
new ApplePredicate() {
public boolean test(Apple apple){
return "red".equals(a.getColor());
}
});
- Very bulky and confusing to use.
Filter Using Lambda Expression
filterApples(inventory, (Apple apple) ->
"red".equals(apple.getColor()));
filterApples(inventory, (Apple apple) ->
"green".equals(apple.getColor()));
filterApples(inventory, (Apple apple) ->
apple.getWeight() > 150);
Refactor Design with Generics
public static <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;
}
List<Apple> redApples = filter(inventory, (Apple apple) ->
"red".equals(apple.getColor()));
List<Integer> evenNumbers = filter(numbers, (Integer i) -> i % 2 == 0);
Real World Examples
Anonymous inner class
inventory.sort(new Comparator<Apple>() {
public int compare(Apple a1, Apple a2){
return a1.getWeight().compareTo(a2.getWeight()); }
});
Lambda
inventory.sort((Apple a1, Apple a2) ->
a1.getWeight().compareTo(a2.getWeight()));
Anonymous inner class
Thread t = new Thread(new Runnable() {
public void run(){
System.out.println("Hello world"); }
});
Lambda
Thread t = new Thread(() -> System.out.println("Hello world"));
Lambda Expressions
• Anonymous function
• Doesn’t have a name
• Has a list of parameters, a body, a return type
(List<String> list) -> list.isEmpty()
() -> new Apple(10)
(Apple a) -> System.out.println(a.getWeight())
(String s) -> s.length()
(int a, int b) -> a * b
(Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight())
Functional Interface
• Lambdas are supported by functional interface
• An interface with single abstract method
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
java.util.function
• Predefined Functional Interfaces
• Function<T,R> : Takes an object of type T and returns R.
• Supplier<T> : Just returns an object of type T.
• Predicate<T> : returns a boolean value based on type T.
• Consumer<T> : performs an action with given type T.
Predicate
• Defines an abstract method test
Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();
List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);
Consumer
• Perform some operation on type T
public interface Consumer<T>{
public void accept(T t);
}
public static <T> void forEach(List<T> list, Consumer<T> c){
for(T i: list){
c.accept(i);
}
}
forEach(Arrays.asList(1,2,3,4,5),(Integer i) -> System.out.println(i));
Function
• Defines an abstract method named apply, takes an argument T
and returns of type R.
public interface Function<T, R>{
public R apply(T t);
}
public static <T, R> List<R> map(List<T> list, Function<T, R> f) {
List<R> result = new ArrayList<>();
for(T s: list){
result.add(f.apply(s));
}
return result;
}
List<Integer> l = map(Arrays.asList("lambdas","in","action"), (String s) -> s.length());
Functional Interface for Primitives
• Performance saving from autoboxing with primitive ones.
public interface IntPredicate{
public boolean test(int t);
}
IntPredicate evenNumbers = (int i) -> i % 2 == 0;
evenNumbers.test(1000);
// true (no boxing)
Predicate<Integer> oddNumbers = (Integer i) -> i % 2 == 1;
oddNumbers.test(1000);
// false (boxing)
Type Interference
• Deduce appropriate signature and target type
List<Apple> greenApples = filter(inventory, (Apple a) ->
"green".equals(a.getColor()));
List<Apple> greenApples = filter(inventory, a -> "green".equals(a.getColor()));
Comparator<Apple> c =(Apple a1, Apple a2) ->
a1.getWeight().compareTo(a2.getWeight());
Comparator<Apple> c = (a1, a2) ->
a1.getWeight().compareTo(a2.getWeight());
Capturing Variables within a Lambda
• The variable should be captured in lambda must be final or
“effectively final”.
int portNumber = 1337;
//error
portNumber = 31338;
Runnable r = () → System.out.println(portNumber);
Method References
• Another syntax for lambda expressions
Lambda Method reference equivalent
(Apple a)-> a.getWeight() Apple::getWeight
()->Thread.currentThread.dumpStack() Thread.currentThread()::dumpStack
(String s) -> System.out.println(s) System.out::println
Stream API
• A sequence of elements from a source that supports
aggregate operations.
• Stream doesn’t hold all data
• Lazily constructed collections
• Uses internal iterations
• Supports parallelism easily.
• java.util.stream
Filter
• Acceps a predicate to filter
stringCollection
.stream()
.filter((s) -> s.startsWith("a"))
.forEach(System.out::println);
Sorted
• Sorted in natural order unless you pass a custom Comparator.
stringCollection
.stream()
.sorted()
.filter((s) -> s.startsWith("a"))
.forEach(System.out::println);
//list interface sort method
inventory.sort((a1, a2) -> a1.getWeight().compareTo(a2.getWeight()));//sort by
weight
Map
• Convert each element into another object.
• Uses Function interface
stringCollection
.stream()
.map(String::toUpperCase)
.sorted((a, b) -> b.compareTo(a))
.forEach(System.out::println);
Reduce
• Perform a reduction on the elements of the stream
List<Integer> numbers = Arrays.asList(3,4,5,1,2);
int sum = numbers.stream().reduce(0, (a, b) -> a + b);
int sum2 = numbers.stream().reduce(0, Integer::sum);
int max = numbers.stream().reduce(0, (a, b) -> Integer.max(a, b));
Match
• returns a boolean
• boolean anyStartsWithA = stringCollection
.stream()
.anyMatch((s) -> s.startsWith("a"));
• anyMatch, allMatch, noneMatch
Other Useful Methods
• distinct > returns a stream of distinct elements
• peek > support debugging of elements
• limit > returns a stream of elements with maxSize
• skip > returns a stream of elements after discarding the first
number of elements of stream.
• min, max, count
• forEach, collect(toList), findFirst, findAny, of
• groupingBy, partitioningBy
Lazy Evaluation
• Lazy intermediate operations: filter, sorted, map,
• Terminal operations: match, count, reduce, collect
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
List<Integer> twoEvenSquares = numbers
.stream()
.filter(n -> n% 2 == 0)
.map(n -> n * n)
.limit(2)
.collect(toList());
System.out.println(twoEvenSquares); // [ 4, 16]
Parallelism
Parallel Stream
• Just use parallelStream method
• We should be careful about the collection size for
performance
List<Apple> greenApples = inventory.parallelStream()
.filter(a -> "green".equals(a.getColor()))
.sorted()
.collect(toList());
Null Reference Problem
• “I call it my billion-dollar mistake. simply because it was so
easy to implement. This has led to innumerable errors,
vulnerabilities, and system crashes, which have probably
caused a billion dollars of pain and damage in the last forty
years.”
Tony Hoare – developed quick sort, ALGOL
Optional Type
Optional<String> reduced = stringCollection
.stream()
.sorted()
.reduce((s1, s2) -> s1 + "#" + s2);
reduced.ifPresent(System.out::println);
Optional Type
Optional<String> optional = Optional.of("test");
optional.isPresent(); // true
optional.get(); // "test"
optional.orElse("fallback"); // "test”
optional.ifPresent((s) -> System.out.println(s.charAt(0))); //
"t"
Default Methods
• Also known as virtual extension methods
• Adds methods to interfaces with implementation
• Mainly aimed for backward compatibility
• Multiple inheritance?, difference from abstract class?
interface Iterable<T> {
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
}
New Date API
• Under the package java.time
• Looks like Joda-Time library
• Immutable classes
LocalTime now1 = LocalTime.now();
LocalTime now2 = LocalTime.now();
System.out.println(now1.isBefore(now2)); // false
LocalDate today = LocalDate.now(); //immutable
LocalDate tomorrow = today.plusDays(1);
LocalDate yesterday = tomorrow.minusDays(2);
LocalDateTime localDateTime = LocalDateTime.now();
Nashorn Javascript Engine
• replaces the old Rhino Javascript engine
• competes with Google V8(powers Chrome and Node.js)
• can be executed command-line with jjs tool or
programmatically
• javascript functions can be called from java side
Nashorn Javascript Engine
var fun1 = function(name) {
print('Hi there from Javascript, ' + name);
return "greetings from javascript";
};
ScriptEngine engine = new
ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader("script.js"));
Invocable invocable = (Invocable) engine;
Object result = invocable.invokeFunction("fun1", ”Liu Kang");
Metaspace
• A new memory space
• Permgen space completely removed
• Allocations for the class metadata are now allocated out of
native memory
• A new flag is available (MaxMetaspaceSize)
Questions?
References
• https://github.com/java8/Java8InAction
• http://winterbe.com/posts/2014/03/16/java-8-tutorial/
• http://blog.informatech.cr/2013/04/10/java-optional-objects/
• http://viralpatel.net/blogs/java-8-default-methods-tutorial/
• http://steve-yegge.blogspot.com.tr/2006/03/execution-in-kingdom-of-nouns.html
• http://en.wikipedia.org/wiki/Tony_Hoare
• http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/
• http://java.dzone.com/articles/java-8-permgen-metaspace
• Subramaniam, Venkat. (2014) Functional Programming in Java: Harnessing the Power Of Java 8 Lambda
Expressions
• Warburton, Richard. (2014) Java 8 Lambdas: Pragmatic Functional Programming
• Urma, Raoul-Gabriel. (2014) Java 8 in Action: Lambdas, Streams and Functional-style Programming

More Related Content

What's hot

Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
shinolajla
 

What's hot (20)

Kotlin Introduction with Android applications
Kotlin Introduction with Android applicationsKotlin Introduction with Android applications
Kotlin Introduction with Android applications
 
JavaScript for ABAP Programmers - 5/7 Functions
JavaScript for ABAP Programmers - 5/7 FunctionsJavaScript for ABAP Programmers - 5/7 Functions
JavaScript for ABAP Programmers - 5/7 Functions
 
Programing for problem solving ( airline reservation system)
Programing for problem solving ( airline reservation system)Programing for problem solving ( airline reservation system)
Programing for problem solving ( airline reservation system)
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
 
Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)
 
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
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
Introduction to programming in scala
Introduction to programming in scalaIntroduction to programming in scala
Introduction to programming in scala
 
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
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
 
Functional Programming In Practice
Functional Programming In PracticeFunctional Programming In Practice
Functional Programming In Practice
 
Java8 features
Java8 featuresJava8 features
Java8 features
 
Functional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and StreamsFunctional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and Streams
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
 
Intro to Functional Programming in Scala
Intro to Functional Programming in ScalaIntro to Functional Programming in Scala
Intro to Functional Programming in Scala
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
 
Java 8 streams
Java 8 streamsJava 8 streams
Java 8 streams
 

Similar to Project Lambda: Evolution of Java

FUNctional Programming in Java 8
FUNctional Programming in Java 8FUNctional Programming in Java 8
FUNctional Programming in Java 8
Richard Walker
 
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
 

Similar to Project Lambda: Evolution of Java (20)

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
 
Java8
Java8Java8
Java8
 
Gdg almaty. Функциональное программирование в Java 8
Gdg almaty. Функциональное программирование в Java 8Gdg almaty. Функциональное программирование в Java 8
Gdg almaty. Функциональное программирование в Java 8
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
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
 
Lazy java
Lazy javaLazy java
Lazy java
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
Java 8
Java 8Java 8
Java 8
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
 
FUNctional Programming in Java 8
FUNctional Programming in Java 8FUNctional Programming in Java 8
FUNctional Programming in Java 8
 
What You Need to Know about Lambdas
What You Need to Know about LambdasWhat You Need to Know about Lambdas
What You Need to Know about Lambdas
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
 
20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
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
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

Project Lambda: Evolution of Java

  • 1. Project Lambda: Evolution of Java Can Pekdemir
  • 2. “I would say, for most Java developers, it’s revolutionary. For polyglot programmers, it’s evolutionary”. Adam Bien - author of Real World Java EE Patterns
  • 3. Agenda • Programming paradigms • Rise of functional programming • Lambda expressions • Functional Interface • Stream API • Other exciting Java 8 features
  • 4. Programming Paradigms • Main programming paradigms • Imperative programming • Functional programming • Logic programming • Object-Oriented Programming?
  • 5. Imperative Programming • Ideas of Von Neumann architecture digital hardware technology • First do this and next do that • Concentrates on how • Mutable variables • Assignments, control structures if-then-else, loops, etc.
  • 6. Imperative Programming Total of discounted prices by then which are greater than 20. BigDecimal totalOfDiscountedPrices = BigDecimal.ZERO; for(BigDecimal price : prices) { if(price.compareTo(BigDecimal.valueOf(20)) > 0) totalOfDiscountedPrices = totalOfDiscountedPrices.add(price.multiply(BigDecimal.valueOf(0.9))); } System.out.println("Total of discounted prices: “ + totalOfDiscountedPrices);
  • 7. Functional Programming • Mathematics and the theory of functions • John Backus:”Can programming be liberated from the von Neumann Style?” • Functions are first class citizens. • Functions can be defined anywhere, inside other functions. • Functions can be passed as argument. • Typically avoids mutable state. • What we want rather than how to do it.
  • 8. Functional Programming final BigDecimal totalOfDiscountedPrices = prices.stream() .filter(price -> price.compareTo(BigDecimal.valueOf(20)) > 0) .map(price -> price.multiply(BigDecimal.valueOf(0.9))) .reduce(BigDecimal.ZERO, BigDecimal::add); Declarative style: expressions over statements.
  • 9. Executionin the Kingdomof Nouns button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("hi " + name); }}); StateManager.getConsiderationSetter("Noun Oriented Thinking", State.HARMFUL).run(); Steve Yegge http://steve-yegge.blogspot.com.tr/2006/03/execution-in- kingdom-of-nouns.html
  • 10. The Difference “OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts.” Michael Feathers, author of “Working with legacy code”
  • 11. Functional Programming Rises • The rise of multicore CPUs and big data. • Need for parallel programming with immutable data. • Declarative programming • Less clutter code
  • 12. Java Evolution • Lack of abstracting over behaviour, not just data • Parallelism should be free, confusing thread api • Need for lazy evaluation over data
  • 13. Behaviour Abstraction • First requirement: filter green apples public static List<Apple> filterGreenApples(List<Apple> inventory){ List<Apple> result = new ArrayList<>(); for (Apple apple: inventory){ if ("green".equals(apple.getColor())) { result.add(apple); } } return result; } What if they want to according to “red”!
  • 14. Behaviour Abstraction • Abstracting over color public static List<Apple> filterApplesByColour(List<Apple> inventory, String color) { List<Apple> result = new ArrayList<>(); for (Apple apple: inventory){ if ( apple.getColor().equals(color) ) { result.add(apple); } } return result; } - What if they want to filter according to weight!
  • 15. Refactoring? public static List<Apple> filterApples(List<Apple> inventory, String color, int weight, boolean flag) { List<Apple> result = new ArrayList<>(); for (Apple apple: inventory){ if ( (flag && apple.getColor().equals(color)) || (!flag && apple.getWeight() > weight) ){ result.add(apple); } } return result; } List<Apple> greenApples = filterApples(inventory, "green", 0, true); List<Apple> heavyApples = filterApples(inventory, "", 150, false);
  • 16. Parameterize Behaviour ApplePredicate encapsulates a strategy for selecting an apple public interface ApplePredicate{ boolean test (Apple apple); } public class AppleWeightPredicate implements ApplePredicate{ public boolean test(Apple apple){ return apple.getWeight() > 150; } } public class AppleColorPredicate implements ApplePredicate{ public boolean test(Apple apple){ return "green".equals(apple.getColor()); } }
  • 17. Filtering by an Abstract Criterion public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate p){ List<Apple> result = new ArrayList<>(); for(Apple apple: inventory){ if(p.test(apple)){ result.add(apple); } } return result; }
  • 18. Filter with Anonymous Class List<Apple> redApples = filterApples(inventory, new ApplePredicate() { public boolean test(Apple apple){ return "red".equals(a.getColor()); } }); - Very bulky and confusing to use.
  • 19. Filter Using Lambda Expression filterApples(inventory, (Apple apple) -> "red".equals(apple.getColor())); filterApples(inventory, (Apple apple) -> "green".equals(apple.getColor())); filterApples(inventory, (Apple apple) -> apple.getWeight() > 150);
  • 20. Refactor Design with Generics public static <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; } List<Apple> redApples = filter(inventory, (Apple apple) -> "red".equals(apple.getColor())); List<Integer> evenNumbers = filter(numbers, (Integer i) -> i % 2 == 0);
  • 21. Real World Examples Anonymous inner class inventory.sort(new Comparator<Apple>() { public int compare(Apple a1, Apple a2){ return a1.getWeight().compareTo(a2.getWeight()); } }); Lambda inventory.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight())); Anonymous inner class Thread t = new Thread(new Runnable() { public void run(){ System.out.println("Hello world"); } }); Lambda Thread t = new Thread(() -> System.out.println("Hello world"));
  • 22. Lambda Expressions • Anonymous function • Doesn’t have a name • Has a list of parameters, a body, a return type (List<String> list) -> list.isEmpty() () -> new Apple(10) (Apple a) -> System.out.println(a.getWeight()) (String s) -> s.length() (int a, int b) -> a * b (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight())
  • 23. Functional Interface • Lambdas are supported by functional interface • An interface with single abstract method @FunctionalInterface public interface Predicate<T> { boolean test(T t); }
  • 24. java.util.function • Predefined Functional Interfaces • Function<T,R> : Takes an object of type T and returns R. • Supplier<T> : Just returns an object of type T. • Predicate<T> : returns a boolean value based on type T. • Consumer<T> : performs an action with given type T.
  • 25. Predicate • Defines an abstract method test Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty(); List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);
  • 26. Consumer • Perform some operation on type T public interface Consumer<T>{ public void accept(T t); } public static <T> void forEach(List<T> list, Consumer<T> c){ for(T i: list){ c.accept(i); } } forEach(Arrays.asList(1,2,3,4,5),(Integer i) -> System.out.println(i));
  • 27. Function • Defines an abstract method named apply, takes an argument T and returns of type R. public interface Function<T, R>{ public R apply(T t); } public static <T, R> List<R> map(List<T> list, Function<T, R> f) { List<R> result = new ArrayList<>(); for(T s: list){ result.add(f.apply(s)); } return result; } List<Integer> l = map(Arrays.asList("lambdas","in","action"), (String s) -> s.length());
  • 28. Functional Interface for Primitives • Performance saving from autoboxing with primitive ones. public interface IntPredicate{ public boolean test(int t); } IntPredicate evenNumbers = (int i) -> i % 2 == 0; evenNumbers.test(1000); // true (no boxing) Predicate<Integer> oddNumbers = (Integer i) -> i % 2 == 1; oddNumbers.test(1000); // false (boxing)
  • 29. Type Interference • Deduce appropriate signature and target type List<Apple> greenApples = filter(inventory, (Apple a) -> "green".equals(a.getColor())); List<Apple> greenApples = filter(inventory, a -> "green".equals(a.getColor())); Comparator<Apple> c =(Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()); Comparator<Apple> c = (a1, a2) -> a1.getWeight().compareTo(a2.getWeight());
  • 30. Capturing Variables within a Lambda • The variable should be captured in lambda must be final or “effectively final”. int portNumber = 1337; //error portNumber = 31338; Runnable r = () → System.out.println(portNumber);
  • 31. Method References • Another syntax for lambda expressions Lambda Method reference equivalent (Apple a)-> a.getWeight() Apple::getWeight ()->Thread.currentThread.dumpStack() Thread.currentThread()::dumpStack (String s) -> System.out.println(s) System.out::println
  • 32. Stream API • A sequence of elements from a source that supports aggregate operations. • Stream doesn’t hold all data • Lazily constructed collections • Uses internal iterations • Supports parallelism easily. • java.util.stream
  • 33. Filter • Acceps a predicate to filter stringCollection .stream() .filter((s) -> s.startsWith("a")) .forEach(System.out::println);
  • 34. Sorted • Sorted in natural order unless you pass a custom Comparator. stringCollection .stream() .sorted() .filter((s) -> s.startsWith("a")) .forEach(System.out::println); //list interface sort method inventory.sort((a1, a2) -> a1.getWeight().compareTo(a2.getWeight()));//sort by weight
  • 35. Map • Convert each element into another object. • Uses Function interface stringCollection .stream() .map(String::toUpperCase) .sorted((a, b) -> b.compareTo(a)) .forEach(System.out::println);
  • 36. Reduce • Perform a reduction on the elements of the stream List<Integer> numbers = Arrays.asList(3,4,5,1,2); int sum = numbers.stream().reduce(0, (a, b) -> a + b); int sum2 = numbers.stream().reduce(0, Integer::sum); int max = numbers.stream().reduce(0, (a, b) -> Integer.max(a, b));
  • 37. Match • returns a boolean • boolean anyStartsWithA = stringCollection .stream() .anyMatch((s) -> s.startsWith("a")); • anyMatch, allMatch, noneMatch
  • 38. Other Useful Methods • distinct > returns a stream of distinct elements • peek > support debugging of elements • limit > returns a stream of elements with maxSize • skip > returns a stream of elements after discarding the first number of elements of stream. • min, max, count • forEach, collect(toList), findFirst, findAny, of • groupingBy, partitioningBy
  • 39. Lazy Evaluation • Lazy intermediate operations: filter, sorted, map, • Terminal operations: match, count, reduce, collect List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); List<Integer> twoEvenSquares = numbers .stream() .filter(n -> n% 2 == 0) .map(n -> n * n) .limit(2) .collect(toList()); System.out.println(twoEvenSquares); // [ 4, 16]
  • 41. Parallel Stream • Just use parallelStream method • We should be careful about the collection size for performance List<Apple> greenApples = inventory.parallelStream() .filter(a -> "green".equals(a.getColor())) .sorted() .collect(toList());
  • 42. Null Reference Problem • “I call it my billion-dollar mistake. simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.” Tony Hoare – developed quick sort, ALGOL
  • 43. Optional Type Optional<String> reduced = stringCollection .stream() .sorted() .reduce((s1, s2) -> s1 + "#" + s2); reduced.ifPresent(System.out::println);
  • 44. Optional Type Optional<String> optional = Optional.of("test"); optional.isPresent(); // true optional.get(); // "test" optional.orElse("fallback"); // "test” optional.ifPresent((s) -> System.out.println(s.charAt(0))); // "t"
  • 45. Default Methods • Also known as virtual extension methods • Adds methods to interfaces with implementation • Mainly aimed for backward compatibility • Multiple inheritance?, difference from abstract class? interface Iterable<T> { default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } }
  • 46. New Date API • Under the package java.time • Looks like Joda-Time library • Immutable classes LocalTime now1 = LocalTime.now(); LocalTime now2 = LocalTime.now(); System.out.println(now1.isBefore(now2)); // false LocalDate today = LocalDate.now(); //immutable LocalDate tomorrow = today.plusDays(1); LocalDate yesterday = tomorrow.minusDays(2); LocalDateTime localDateTime = LocalDateTime.now();
  • 47. Nashorn Javascript Engine • replaces the old Rhino Javascript engine • competes with Google V8(powers Chrome and Node.js) • can be executed command-line with jjs tool or programmatically • javascript functions can be called from java side
  • 48. Nashorn Javascript Engine var fun1 = function(name) { print('Hi there from Javascript, ' + name); return "greetings from javascript"; }; ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval(new FileReader("script.js")); Invocable invocable = (Invocable) engine; Object result = invocable.invokeFunction("fun1", ”Liu Kang");
  • 49. Metaspace • A new memory space • Permgen space completely removed • Allocations for the class metadata are now allocated out of native memory • A new flag is available (MaxMetaspaceSize)
  • 51. References • https://github.com/java8/Java8InAction • http://winterbe.com/posts/2014/03/16/java-8-tutorial/ • http://blog.informatech.cr/2013/04/10/java-optional-objects/ • http://viralpatel.net/blogs/java-8-default-methods-tutorial/ • http://steve-yegge.blogspot.com.tr/2006/03/execution-in-kingdom-of-nouns.html • http://en.wikipedia.org/wiki/Tony_Hoare • http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/ • http://java.dzone.com/articles/java-8-permgen-metaspace • Subramaniam, Venkat. (2014) Functional Programming in Java: Harnessing the Power Of Java 8 Lambda Expressions • Warburton, Richard. (2014) Java 8 Lambdas: Pragmatic Functional Programming • Urma, Raoul-Gabriel. (2014) Java 8 in Action: Lambdas, Streams and Functional-style Programming