SlideShare a Scribd company logo
Software
Engineering
Computer Science
Engineering School
Francisco Ortin University of Oviedo
New Functional Features
of Java 8
Francisco Ortín Soler
Disclaimer
• This slides are aimed at briefly explaining the new
functional features of Java 8
• It is an informal document
• The code used in this slides is available at
http://www.reflection.uniovi.es/download/2014/java8.zip
• It has been compiled and executed with Java SE
Development Kit 8.0
March 20th, 2014
Francisco Ortin
ortin at lsi.uniovi.es
Francisco Ortín Soler
Java 8
• Java 8 has been released on March 2014
• It includes some features of the functional
paradigm such as:
 Lambda expressions
 Method references
 Types of some typical lambda expressions
 Streams (aggregate operations)
 Closures (constant variables of the enclosing block)
• It also provides default method implementations
for interfaces
Francisco Ortín Soler
Lambda Expressions
• Lambda expressions are provided
 The -> symbol separates parameters from body
 Parameter types can be optionally specified
 Parenthesis are not mandatory when only one
parameter is passed
 If the body is just one expression, return and { }
are not required
String[] words = new String [] {
"Hello", "from", "Java", "8" };
Arrays.sort(words,
(word1, word2) -> word1.length() - word2.length()
);
Francisco Ortín Soler
Types of Lambda Expressions
• Lambda expressions promote to interfaces with
one abstract method with the same signature as
the lambda expression
• This kind of interfaces are called Functional
Interfaces
 The @FunctionalInterface annotation can be used
 It is optional; helpful for detecting errors
@FunctionalInterface // not mandatory
interface MyPredicate<T> {
boolean exec(T element);
}
Francisco Ortín Soler
Types of Lambda Expressions
@FunctionalInterface // not mandatory
interface MyPredicate<T> {
boolean exec(T element);
}
class Promotion {
static <T> T find(T[] collection, MyPredicate<T> predicate) {
for(T item : collection)
if (predicate.exec(item))
return item;
return null;
}
public static void main(String... args) {
Integer[] numbers = new Integer [] { 1, 2, 3 };
int number = find(numbers, n -> n % 2 == 0);
System.out.println(number);
}
}
Francisco Ortín Soler
Method References
• Sometimes, a lambda expression does nothing but calling
an existing method
• In those cases, the existing method can be referred by
name
• For this purpose, the :: operator has been added to Java 8
• Static (class) methods are referred with Class::method
class MethodReferences {
static boolean isOdd(Integer number) {
return number %2 != 0;
}
public static void main(String... args) {
Integer[] numbers = new Integer [] { 1, 2, 3 };
Integer number = Promotion.find(numbers, MethodReferences::isOdd);
number = Promotion.find(numbers, new EqualTo(3)::compare);
}
}
Francisco Ortín Soler
Method References
• Instance methods are referred with object::method
• Since these methods are associated to an object (this),
they can be stateful
class EqualTo {
private int value;
public EqualTo(int value) { this.value = value; }
public boolean compare(Integer n) { return value == n; }
}
public class MethodReferences {
public static void main(String... args) {
Integer[] numbers = new Integer [] { 1, 2, 3 };
Integer number = Promotion.find(numbers,
new EqualTo(3)::compare);
}
}
Francisco Ortín Soler
Types of Typical Lambda Exprs
• The package java.util.function provides types
(functional interfaces) of typical lambda functions
 Function<T,R>: Function that receives a T argument
and returns a R result
 Predicate<T>: Predicate of one T argument
 Consumer<T>: An operation that accepts a single T
argument and returns no result
 Supplier<T>: Function with no parameter returning
a T value
 UnaryOperator<T>: Operation on a single T operand,
producing a T result
 BinaryOperator<T>: Operation upon two T
operands, producing a result of the same type as
the operands
Francisco Ortín Soler
Types of Typical Lambda Exprs
• Notice: the methods of the interfaces must be explicitly called, and
they are named differently (test, accept, apply, get…)
public static void main(String... args) {
MyPredicate<Integer> even = n -> n%2 == 0; // my own type
Predicate<Integer> odd = n -> n%2 != 0;
System.out.println(even.exec(number) + " " + odd.test(number));
Consumer<Integer> printAction = n -> System.out.println(n);
printAction.accept(number);
Function<Integer,Double> sqrt = n -> Math.sqrt(n);
System.out.println(sqrt.apply(number));
Supplier<Integer> random = () -> (int)(Math.random()*1000 - 1000/2);
System.out.println(random.get());
BinaryOperator<Integer> times = (a,b) -> a*b;
System.out.printf(times.apply(3,2));
Francisco Ortín Soler
Types of Typical Lambda Exprs
• Since generics is implemented in Java with type
erasure (i.e., T is Object), the previous types
have specific versions for built-in types:
And more…
http://download.java.net/jdk8/docs/api/java/util/function/package-summary.html
Predicate<T> Supplier<T> Consumer<T> Function<T,R>
DoublePredicate BooleanSupplier DoubleConsumer DoubleFunction<R>
IntPredicate DoubleSupplier IntConsumer IntFunction<R>
LongPredicate IntSupplier LongConsumer IntToDoubleFunction
LongSupplier IntToLongFunction
LongFunction<R>
…
Francisco Ortín Soler
Streams with Aggregate Operations
• The new java.util.stream package provides an API to
support functional-style operations on streams
• A stream is a sequence of elements
 It is not a data structure that stores elements (i.e. a
collection)
• They support sequential and parallel functional-style
aggregate operations
• Operations are composed into a stream pipeline
• Pipeline consists of
 A source (array, collection, generator, I/O channel…)
 Intermediate aggregate operations
 And a terminal operation, producing a result
• Computation on the source data is only performed when
the terminal operation is initiated (kind of lazy)
Francisco Ortín Soler
Streams (Aggregate Operations)
public class Streams {
static int compute(Collection<Integer> collection) {
return collection.stream()
.filter(n -> n%2 == 0) // even numbers
.map(n -> n*n) // square
.reduce(0, (acc, item) -> acc + item); // summation
}
public static void main(String... args) {
System.out.println(compute(Arrays.asList(1, 2, 3, 4, 5)));
System.out.println(Arrays.asList(
Stream.iterate(1, n -> n+1)
.skip(10)
.limit(5)
.toArray(Integer[]::new)
));
}
}
source
aggregate operations
terminal operation
source (infinite)
aggregate operations
terminal operation
• Similar to .NET LINQ
• There will be database streams eventually?
Francisco Ortín Soler
Closures
• Lambda expressions can capture variables of the
enclosing scope
• They do not have shadowing issues (a new scope is not
created, being lexically scoped)
• Captured variables must be final or effectively final (their
value cannot be modified)
public class Closures {
static Function<Integer,Integer> createClosure(int initialValue) {
int number = initialValue; // must be constant
return n -> number + n;
}
public static void main(String... args) {
Function<Integer,Integer> closure1 = createClosure(1);
System.out.println(closure1.apply(7) );
Function<Integer,Integer> closure10 = createClosure(10);
System.out.println(closure10.apply(7) );
}
}
Francisco Ortín Soler
Closures
• Since functions are objects, they can represent functions
with a mutable state
class Fibonacci implements Supplier<Integer> {
private int previous = 0, current = 1;
@Override
public Integer get() {
int next = current + previous;
previous = current;
current = next;
return previous;
}
public static void main(String... args) {
System.out.println(Arrays.asList(
Stream.generate(new Fibonacci()).limit(10)
.toArray(Integer[]::new)
));
}
}
Francisco Ortín Soler
Default Methods
• Java 8 provides default implementations for interface
methods (the default keyword is used), similar to mixins
@FunctionalInterface interface Comparator<T> {
int compare(T a, T b);
default Comparator<T> reversed() { return (a, b) -> this.compare(b,a); }
}
public class DefaultMethods {
public static <T> T max(T a, T b, Comparator<T> comp) {
return comp.compare(a,b)<0 ? a : b;
}
public static <T> T min(T a, T b, Comparator<T> comp) {
return max(a, b, comp.reversed());
}
public static void main(String... args) {
Comparator<String> comparator = (a,b) -> a.length() - b.length();
System.out.println(max("hello", "bye", comparator));
System.out.println(min("hello", "bye", comparator));
} }
Francisco Ortín Soler
Multiple Inheritance
• As with multiple inheritance languages, different
implementations of the same method may be
inherited
• However, the Java compiler checks this condition,
reporting an error
interface A {
default void m() { System.out.println("A::m"); }
}
interface B {
default void m() { System.out.println("B::m"); }
}
public class MultipleInheritance
implements A, B { // compiler error
}
Francisco Ortín Soler
Multiple Inheritance
• Besides, a default method cannot be inherited if
the class implements another interface with that
method (even without a default implementation)
interface A {
default void m() { System.out.println("A::m"); }
}
interface C {
void m();
}
class MyClass implements A, C { // compiler error
}
Francisco Ortín Soler
Multiple Inheritance
• Java 8 allows diamond inheritance: the most
specific (derived) method implementation is called
interface A {
default void m() { System.out.println("A::m"); }
}
interface A1 extends A {}
interface A2 extends A {
default void m() { System.out.println("A2::m"); }
}
class Diamond implements A1, A2 {
public static void main(String... args) {
new Diamond().m(); // A2::m
A1 a1 = new Diamond();
a1.m();
} }
Francisco Ortín Soler
Static Methods
• Java 8 allows interfaces to implement static methods to
provide utility methods
• The static methods specific to an interface can be kept in
the same interface rather than in a separate class
@FunctionalInterface
interface Comparator<T> {
int compare(T a, T b);
static <T extends Comparable<T>> Comparator<T> naturalOrder() {
return (a,b) -> a.compareTo(b);
}
}
public class DefaultMethods {
public static void main(String... args) {
System.out.println(
max("hello", "bye", Comparator.naturalOrder()
));
} }
Software
Engineering
Computer Science
Engineering School
Francisco Ortin University of Oviedo
New Functional Features
of Java 8
Ad

More Related Content

What's hot (20)

Java simple programs
Java simple programsJava simple programs
Java simple programs
VEERA RAGAVAN
 
Java generics
Java genericsJava generics
Java generics
Hosein Zare
 
Introduction to Erlang Part 1
Introduction to Erlang Part 1Introduction to Erlang Part 1
Introduction to Erlang Part 1
Dmitry Zinoviev
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
knutmork
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
Erhan Bagdemir
 
Introduction to Java Programming Part 2
Introduction to Java Programming Part 2Introduction to Java Programming Part 2
Introduction to Java Programming Part 2
university of education,Lahore
 
Java operators
Java operatorsJava operators
Java operators
Shehrevar Davierwala
 
Introduction to Erlang Part 2
Introduction to Erlang Part 2Introduction to Erlang Part 2
Introduction to Erlang Part 2
Dmitry Zinoviev
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling
Intro C# Book
 
Java and j2ee_lab-manual
Java and j2ee_lab-manualJava and j2ee_lab-manual
Java and j2ee_lab-manual
hanumanthu mothukuru
 
Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on Session
Dharmesh Tank
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of Lambdas
Esther Lozano
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
Urs Peter
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Hyderabad Scalability Meetup
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
AravindSankaran
 
Wien15 java8
Wien15 java8Wien15 java8
Wien15 java8
Jaanus Pöial
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
langer4711
 
02basics
02basics02basics
02basics
Waheed Warraich
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
Intro C# Book
 
Java simple programs
Java simple programsJava simple programs
Java simple programs
VEERA RAGAVAN
 
Introduction to Erlang Part 1
Introduction to Erlang Part 1Introduction to Erlang Part 1
Introduction to Erlang Part 1
Dmitry Zinoviev
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
knutmork
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
Erhan Bagdemir
 
Introduction to Erlang Part 2
Introduction to Erlang Part 2Introduction to Erlang Part 2
Introduction to Erlang Part 2
Dmitry Zinoviev
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling
Intro C# Book
 
Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on Session
Dharmesh Tank
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of Lambdas
Esther Lozano
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
Urs Peter
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
langer4711
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
Intro C# Book
 

Similar to New Functional Features of Java 8 (20)

Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
Aniket Thakur
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
MSohaib24
 
Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
Lambda Functions in Java 8
Lambda Functions in Java 8Lambda Functions in Java 8
Lambda Functions in Java 8
Ganesh Samarthyam
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programming
Henri Tremblay
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
Vincent Pradeilles
 
[Codemotion 2015] patrones de diseño con java8
[Codemotion 2015] patrones de diseño con java8[Codemotion 2015] patrones de diseño con java8
[Codemotion 2015] patrones de diseño con java8
Alonso Torres
 
Java8
Java8Java8
Java8
Felipe Mamud
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
Mario Fusco
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
Tomasz Kowalczewski
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
jessitron
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
Olexandra Dmytrenko
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
Martin Toshev
 
Gdg almaty. Функциональное программирование в Java 8
Gdg almaty. Функциональное программирование в Java 8Gdg almaty. Функциональное программирование в Java 8
Gdg almaty. Функциональное программирование в Java 8
Madina Kamzina
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
Intro C# Book
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data Management
Albert Bifet
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
Saugat Gautam
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
MSohaib24
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programming
Henri Tremblay
 
[Codemotion 2015] patrones de diseño con java8
[Codemotion 2015] patrones de diseño con java8[Codemotion 2015] patrones de diseño con java8
[Codemotion 2015] patrones de diseño con java8
Alonso Torres
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
jessitron
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
Martin Toshev
 
Gdg almaty. Функциональное программирование в Java 8
Gdg almaty. Функциональное программирование в Java 8Gdg almaty. Функциональное программирование в Java 8
Gdg almaty. Функциональное программирование в Java 8
Madina Kamzina
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
Intro C# Book
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data Management
Albert Bifet
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
Saugat Gautam
 
Ad

Recently uploaded (20)

Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Ad

New Functional Features of Java 8

  • 1. Software Engineering Computer Science Engineering School Francisco Ortin University of Oviedo New Functional Features of Java 8
  • 2. Francisco Ortín Soler Disclaimer • This slides are aimed at briefly explaining the new functional features of Java 8 • It is an informal document • The code used in this slides is available at http://www.reflection.uniovi.es/download/2014/java8.zip • It has been compiled and executed with Java SE Development Kit 8.0 March 20th, 2014 Francisco Ortin ortin at lsi.uniovi.es
  • 3. Francisco Ortín Soler Java 8 • Java 8 has been released on March 2014 • It includes some features of the functional paradigm such as:  Lambda expressions  Method references  Types of some typical lambda expressions  Streams (aggregate operations)  Closures (constant variables of the enclosing block) • It also provides default method implementations for interfaces
  • 4. Francisco Ortín Soler Lambda Expressions • Lambda expressions are provided  The -> symbol separates parameters from body  Parameter types can be optionally specified  Parenthesis are not mandatory when only one parameter is passed  If the body is just one expression, return and { } are not required String[] words = new String [] { "Hello", "from", "Java", "8" }; Arrays.sort(words, (word1, word2) -> word1.length() - word2.length() );
  • 5. Francisco Ortín Soler Types of Lambda Expressions • Lambda expressions promote to interfaces with one abstract method with the same signature as the lambda expression • This kind of interfaces are called Functional Interfaces  The @FunctionalInterface annotation can be used  It is optional; helpful for detecting errors @FunctionalInterface // not mandatory interface MyPredicate<T> { boolean exec(T element); }
  • 6. Francisco Ortín Soler Types of Lambda Expressions @FunctionalInterface // not mandatory interface MyPredicate<T> { boolean exec(T element); } class Promotion { static <T> T find(T[] collection, MyPredicate<T> predicate) { for(T item : collection) if (predicate.exec(item)) return item; return null; } public static void main(String... args) { Integer[] numbers = new Integer [] { 1, 2, 3 }; int number = find(numbers, n -> n % 2 == 0); System.out.println(number); } }
  • 7. Francisco Ortín Soler Method References • Sometimes, a lambda expression does nothing but calling an existing method • In those cases, the existing method can be referred by name • For this purpose, the :: operator has been added to Java 8 • Static (class) methods are referred with Class::method class MethodReferences { static boolean isOdd(Integer number) { return number %2 != 0; } public static void main(String... args) { Integer[] numbers = new Integer [] { 1, 2, 3 }; Integer number = Promotion.find(numbers, MethodReferences::isOdd); number = Promotion.find(numbers, new EqualTo(3)::compare); } }
  • 8. Francisco Ortín Soler Method References • Instance methods are referred with object::method • Since these methods are associated to an object (this), they can be stateful class EqualTo { private int value; public EqualTo(int value) { this.value = value; } public boolean compare(Integer n) { return value == n; } } public class MethodReferences { public static void main(String... args) { Integer[] numbers = new Integer [] { 1, 2, 3 }; Integer number = Promotion.find(numbers, new EqualTo(3)::compare); } }
  • 9. Francisco Ortín Soler Types of Typical Lambda Exprs • The package java.util.function provides types (functional interfaces) of typical lambda functions  Function<T,R>: Function that receives a T argument and returns a R result  Predicate<T>: Predicate of one T argument  Consumer<T>: An operation that accepts a single T argument and returns no result  Supplier<T>: Function with no parameter returning a T value  UnaryOperator<T>: Operation on a single T operand, producing a T result  BinaryOperator<T>: Operation upon two T operands, producing a result of the same type as the operands
  • 10. Francisco Ortín Soler Types of Typical Lambda Exprs • Notice: the methods of the interfaces must be explicitly called, and they are named differently (test, accept, apply, get…) public static void main(String... args) { MyPredicate<Integer> even = n -> n%2 == 0; // my own type Predicate<Integer> odd = n -> n%2 != 0; System.out.println(even.exec(number) + " " + odd.test(number)); Consumer<Integer> printAction = n -> System.out.println(n); printAction.accept(number); Function<Integer,Double> sqrt = n -> Math.sqrt(n); System.out.println(sqrt.apply(number)); Supplier<Integer> random = () -> (int)(Math.random()*1000 - 1000/2); System.out.println(random.get()); BinaryOperator<Integer> times = (a,b) -> a*b; System.out.printf(times.apply(3,2));
  • 11. Francisco Ortín Soler Types of Typical Lambda Exprs • Since generics is implemented in Java with type erasure (i.e., T is Object), the previous types have specific versions for built-in types: And more… http://download.java.net/jdk8/docs/api/java/util/function/package-summary.html Predicate<T> Supplier<T> Consumer<T> Function<T,R> DoublePredicate BooleanSupplier DoubleConsumer DoubleFunction<R> IntPredicate DoubleSupplier IntConsumer IntFunction<R> LongPredicate IntSupplier LongConsumer IntToDoubleFunction LongSupplier IntToLongFunction LongFunction<R> …
  • 12. Francisco Ortín Soler Streams with Aggregate Operations • The new java.util.stream package provides an API to support functional-style operations on streams • A stream is a sequence of elements  It is not a data structure that stores elements (i.e. a collection) • They support sequential and parallel functional-style aggregate operations • Operations are composed into a stream pipeline • Pipeline consists of  A source (array, collection, generator, I/O channel…)  Intermediate aggregate operations  And a terminal operation, producing a result • Computation on the source data is only performed when the terminal operation is initiated (kind of lazy)
  • 13. Francisco Ortín Soler Streams (Aggregate Operations) public class Streams { static int compute(Collection<Integer> collection) { return collection.stream() .filter(n -> n%2 == 0) // even numbers .map(n -> n*n) // square .reduce(0, (acc, item) -> acc + item); // summation } public static void main(String... args) { System.out.println(compute(Arrays.asList(1, 2, 3, 4, 5))); System.out.println(Arrays.asList( Stream.iterate(1, n -> n+1) .skip(10) .limit(5) .toArray(Integer[]::new) )); } } source aggregate operations terminal operation source (infinite) aggregate operations terminal operation • Similar to .NET LINQ • There will be database streams eventually?
  • 14. Francisco Ortín Soler Closures • Lambda expressions can capture variables of the enclosing scope • They do not have shadowing issues (a new scope is not created, being lexically scoped) • Captured variables must be final or effectively final (their value cannot be modified) public class Closures { static Function<Integer,Integer> createClosure(int initialValue) { int number = initialValue; // must be constant return n -> number + n; } public static void main(String... args) { Function<Integer,Integer> closure1 = createClosure(1); System.out.println(closure1.apply(7) ); Function<Integer,Integer> closure10 = createClosure(10); System.out.println(closure10.apply(7) ); } }
  • 15. Francisco Ortín Soler Closures • Since functions are objects, they can represent functions with a mutable state class Fibonacci implements Supplier<Integer> { private int previous = 0, current = 1; @Override public Integer get() { int next = current + previous; previous = current; current = next; return previous; } public static void main(String... args) { System.out.println(Arrays.asList( Stream.generate(new Fibonacci()).limit(10) .toArray(Integer[]::new) )); } }
  • 16. Francisco Ortín Soler Default Methods • Java 8 provides default implementations for interface methods (the default keyword is used), similar to mixins @FunctionalInterface interface Comparator<T> { int compare(T a, T b); default Comparator<T> reversed() { return (a, b) -> this.compare(b,a); } } public class DefaultMethods { public static <T> T max(T a, T b, Comparator<T> comp) { return comp.compare(a,b)<0 ? a : b; } public static <T> T min(T a, T b, Comparator<T> comp) { return max(a, b, comp.reversed()); } public static void main(String... args) { Comparator<String> comparator = (a,b) -> a.length() - b.length(); System.out.println(max("hello", "bye", comparator)); System.out.println(min("hello", "bye", comparator)); } }
  • 17. Francisco Ortín Soler Multiple Inheritance • As with multiple inheritance languages, different implementations of the same method may be inherited • However, the Java compiler checks this condition, reporting an error interface A { default void m() { System.out.println("A::m"); } } interface B { default void m() { System.out.println("B::m"); } } public class MultipleInheritance implements A, B { // compiler error }
  • 18. Francisco Ortín Soler Multiple Inheritance • Besides, a default method cannot be inherited if the class implements another interface with that method (even without a default implementation) interface A { default void m() { System.out.println("A::m"); } } interface C { void m(); } class MyClass implements A, C { // compiler error }
  • 19. Francisco Ortín Soler Multiple Inheritance • Java 8 allows diamond inheritance: the most specific (derived) method implementation is called interface A { default void m() { System.out.println("A::m"); } } interface A1 extends A {} interface A2 extends A { default void m() { System.out.println("A2::m"); } } class Diamond implements A1, A2 { public static void main(String... args) { new Diamond().m(); // A2::m A1 a1 = new Diamond(); a1.m(); } }
  • 20. Francisco Ortín Soler Static Methods • Java 8 allows interfaces to implement static methods to provide utility methods • The static methods specific to an interface can be kept in the same interface rather than in a separate class @FunctionalInterface interface Comparator<T> { int compare(T a, T b); static <T extends Comparable<T>> Comparator<T> naturalOrder() { return (a,b) -> a.compareTo(b); } } public class DefaultMethods { public static void main(String... args) { System.out.println( max("hello", "bye", Comparator.naturalOrder() )); } }
  • 21. Software Engineering Computer Science Engineering School Francisco Ortin University of Oviedo New Functional Features of Java 8