SlideShare a Scribd company logo
Java 8: Language enhancements
Skelia
Agenda
 Lambda
 Method References
 Default methods
 Static methods
 Streams (bulk collection operations)
Lambda - ?
Expression describing the
anonymous function, the result of the
execution of which is the object of
unknown origin, which implements the
required functional interface
Lambda. Examples
 Java < 8
Comparator<Integer> cmp = new Comparator<Integer>() {
@Override
public int compare(Integer x, Integer y) {
return (x < y) ? -1 : (x > y) ? 1 : 0;
}
};
 Java 8
Comparator<Integer> cmp =
(x, y) -> (x < y) ? -1 : (x > y) ? 1 : 0;
Lambda. More examples
 //interface Function<T, R> - R apply(T t);
Function<String, Integer> f0 =
(String s) -> Integer.parseInt(s);
 //interface Supplier <T> - T get();
Supplier<Integer> answerFactory = () -> 42;
 // interface Supplier<T> - T get();
Supplier<Integer> deepThought = () -> {
long millis = TimeUnit.DAYS.toMillis(2737500000L);
Thread.sleep(toThinkInMillis);
return 42;
};
Method References
Easy-to-read lambda expressions for
methods that already have a name
Method references. Precondition
public class Member {
public static int compareByAge(Member a, Member b) {
return a.getBDay().compareTo(b.getBDay());
}
}
Member[] rosterAsArray = /* init array of Members */;
Method references. Java < 8
class MemberAgeComparator implements Comparator<Member> {
public int compare(Member a, Member b) {
return Member.compareByAge(a, b);
}
}
Arrays.sort(rosterAsArray, new MemberAgeComparator());
Method references. Java 8 with Lambda
Arrays.sort(rosterAsArray, (a, b) ->
Member.compareByAge(a, b));
Method reference
Arrays.sort(rosterAsArray, Member::compareByAge);
Method References. Types
Type Example
Reference to a static method ContainingClass::staticMethodName
Reference to an instance method of
a particular object
ContainingObject::instanceMethodNa
me
memberInstance::compareByName;
(memberInstance.compareByName(a, b))
Reference to an instance method of
an arbitrary object of a particular
type
ContainingType::methodName
String::compareToIgnoreCase
(a.compareToIgnoreCase(b))
Reference to a constructor ClassName::new
HashSet::new
(() -> { return new HashSet<>(); })
Default methods. The problem
interface Collection<T> {
/* @since 1.8 */
void removeAll(Predicate<T> p);
}
Default methods. Solution
interface Collection<T> {
/* @since 1.8 */
default void removeAll(Predicate<T> p) {
// fallback implementation goes here
}
}
Default methods. Behavior
Static methods
public interface Ticket {
String qDublin ();
static Ticket random () {
return () -> " toDublin ";
}
}
assertEquals ("toDublin", Ticket.random().qDublin ());
Streams. Problem
public void printGroups(List<People> people) {
Set<Group> groups = new HashSet<>();
for (Person p : people) {
if (p.getAge() >= 65) groups.add(p.getGroup());
}
List<Group> sorted = new ArrayList <>(groups);
Collections.sort(sorted, new Comparator <Group>() {
public int compare (Group a, Group b) {
return Integer.compare(a.getSize(), b.getSize());
}
});
for (Group g : sorted)
System.out.println(g.getName());
}
Streams. Solution
public void printGroups (List<People> people) {
people.stream()
.filter(p -> p.getAge() > 65)
.map(p -> p.getGroup())
.distinct()
.sorted(comparing(g -> g.getSize())
.map(g -> g.getName())
.forEach(n -> System.out.println(n));
}
Stream → sequence of elements.
Streams. Design
source op op … sink→ → → →
 sources: collections, iterators, channels, ...
 operations: filter, map, reduce, ...
 sinks: collectors, forEach, iterator...
Streams. Parallelism
int v = list.parallelStream()
.reduce(Math::max)
.get ();
 Implicit invocation of parallelStream() instead of
stream()
 Implementation is hidden
 Uses ForkJoinPool
Java 8 is coming...
Scheduled to release in March 2014
Questions?
Thank you!
Ad

More Related Content

What's hot (20)

Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Scott Leberknight
 
Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)
Trisha Gee
 
Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8
RichardWarburton
 
Java Generics
Java GenericsJava Generics
Java Generics
Zülfikar Karakaya
 
Introduction to JQ
Introduction to JQIntroduction to JQ
Introduction to JQ
Knoldus Inc.
 
Java 8: the good parts!
Java 8: the good parts!Java 8: the good parts!
Java 8: the good parts!
Andrzej Grzesik
 
Java generics
Java genericsJava generics
Java generics
Hosein Zare
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
Knoldus Inc.
 
Writing beautiful code with Java 8
Writing beautiful code with Java 8Writing beautiful code with Java 8
Writing beautiful code with Java 8
Sergiu Mircea Indrie
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of Lambdas
Esther Lozano
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
knutmork
 
Java operators
Java operatorsJava operators
Java operators
Shehrevar Davierwala
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
Tomasz Kowalczewski
 
Java programs
Java programsJava programs
Java programs
Dr.M.Karthika parthasarathy
 
Scala functions
Scala functionsScala functions
Scala functions
Knoldus Inc.
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
Knoldus Inc.
 
jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a Boss
Bob Tiernay
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
Mario Fusco
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Hyderabad Scalability Meetup
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
Erhan Bagdemir
 

Viewers also liked (20)

Hello Java 8
Hello Java 8Hello Java 8
Hello Java 8
Adam Davis
 
Java 8
Java 8Java 8
Java 8
Raghda Salah
 
Lambda expressions java8 - yousry
Lambda expressions java8 - yousryLambda expressions java8 - yousry
Lambda expressions java8 - yousry
yousry ibrahim
 
Java8 training - class 2
Java8 training - class 2Java8 training - class 2
Java8 training - class 2
Marut Singh
 
Java8
Java8Java8
Java8
Freeman Zhang
 
Java8
Java8Java8
Java8
fbenault
 
Какого сотрудника «оторвут с руками»
Какого сотрудника «оторвут с руками»Какого сотрудника «оторвут с руками»
Какого сотрудника «оторвут с руками»
Natalia Penkina
 
B nye online-coursesyllabus
B nye online-coursesyllabusB nye online-coursesyllabus
B nye online-coursesyllabus
Bobbi Nye
 
分散システム読書会 13章-分散協調ベースシステム
分散システム読書会 13章-分散協調ベースシステム分散システム読書会 13章-分散協調ベースシステム
分散システム読書会 13章-分散協調ベースシステム
Ichiro TAKAHASHI
 
Bingoppt
BingopptBingoppt
Bingoppt
Shravani Nutalapati
 
African dwarf frogs
African dwarf frogsAfrican dwarf frogs
African dwarf frogs
worsleyl
 
AKU LULUSAN STATISTIKA DAN AKU BANGGA! Oleh Arsyil Hendra Saputra
AKU LULUSAN STATISTIKA DAN AKU BANGGA! Oleh Arsyil Hendra SaputraAKU LULUSAN STATISTIKA DAN AKU BANGGA! Oleh Arsyil Hendra Saputra
AKU LULUSAN STATISTIKA DAN AKU BANGGA! Oleh Arsyil Hendra Saputra
Arsyil Hendra Saputra
 
Congratulations to you, mom!
Congratulations to you, mom!Congratulations to you, mom!
Congratulations to you, mom!
janiscoachman
 
B nye online-coursepp
B nye online-courseppB nye online-coursepp
B nye online-coursepp
Bobbi Nye
 
分散システム読書会 06章-同期(後編)
分散システム読書会 06章-同期(後編)分散システム読書会 06章-同期(後編)
分散システム読書会 06章-同期(後編)
Ichiro TAKAHASHI
 
Java8 training - class 3
Java8 training - class 3Java8 training - class 3
Java8 training - class 3
Marut Singh
 
第4回 OSS運用管理勉強会(2014/02/04) 発表資料
第4回 OSS運用管理勉強会(2014/02/04) 発表資料第4回 OSS運用管理勉強会(2014/02/04) 発表資料
第4回 OSS運用管理勉強会(2014/02/04) 発表資料
Ichiro TAKAHASHI
 
Java8
Java8Java8
Java8
Jaime L. López Carratalá
 
Lambda expressions java8 - yousry
Lambda expressions java8 - yousryLambda expressions java8 - yousry
Lambda expressions java8 - yousry
yousry ibrahim
 
Java8 training - class 2
Java8 training - class 2Java8 training - class 2
Java8 training - class 2
Marut Singh
 
Какого сотрудника «оторвут с руками»
Какого сотрудника «оторвут с руками»Какого сотрудника «оторвут с руками»
Какого сотрудника «оторвут с руками»
Natalia Penkina
 
B nye online-coursesyllabus
B nye online-coursesyllabusB nye online-coursesyllabus
B nye online-coursesyllabus
Bobbi Nye
 
分散システム読書会 13章-分散協調ベースシステム
分散システム読書会 13章-分散協調ベースシステム分散システム読書会 13章-分散協調ベースシステム
分散システム読書会 13章-分散協調ベースシステム
Ichiro TAKAHASHI
 
African dwarf frogs
African dwarf frogsAfrican dwarf frogs
African dwarf frogs
worsleyl
 
AKU LULUSAN STATISTIKA DAN AKU BANGGA! Oleh Arsyil Hendra Saputra
AKU LULUSAN STATISTIKA DAN AKU BANGGA! Oleh Arsyil Hendra SaputraAKU LULUSAN STATISTIKA DAN AKU BANGGA! Oleh Arsyil Hendra Saputra
AKU LULUSAN STATISTIKA DAN AKU BANGGA! Oleh Arsyil Hendra Saputra
Arsyil Hendra Saputra
 
Congratulations to you, mom!
Congratulations to you, mom!Congratulations to you, mom!
Congratulations to you, mom!
janiscoachman
 
B nye online-coursepp
B nye online-courseppB nye online-coursepp
B nye online-coursepp
Bobbi Nye
 
分散システム読書会 06章-同期(後編)
分散システム読書会 06章-同期(後編)分散システム読書会 06章-同期(後編)
分散システム読書会 06章-同期(後編)
Ichiro TAKAHASHI
 
Java8 training - class 3
Java8 training - class 3Java8 training - class 3
Java8 training - class 3
Marut Singh
 
第4回 OSS運用管理勉強会(2014/02/04) 発表資料
第4回 OSS運用管理勉強会(2014/02/04) 発表資料第4回 OSS運用管理勉強会(2014/02/04) 発表資料
第4回 OSS運用管理勉強会(2014/02/04) 発表資料
Ichiro TAKAHASHI
 
Ad

Similar to Java8: Language Enhancements (20)

Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8
Dian Aditya
 
Java SE 8
Java SE 8Java SE 8
Java SE 8
Murali Pachiyappan
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
Urs Peter
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
Sanjoy Kumar Roy
 
Java8
Java8Java8
Java8
Sunil Kumar
 
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
Sharon Rozinsky
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
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
 
[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
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
franciscoortin
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
Olexandra Dmytrenko
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
Ganesh Samarthyam
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
CodeOps Technologies LLP
 
Java 8
Java 8Java 8
Java 8
Sheeban Singaram
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
Raffi Khatchadourian
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
New York City College of Technology Computer Systems Technology Colloquium
 
Lambda Functions in Java 8
Lambda Functions in Java 8Lambda Functions in Java 8
Lambda Functions in Java 8
Ganesh Samarthyam
 
Java 8 Feature Preview
Java 8 Feature PreviewJava 8 Feature Preview
Java 8 Feature Preview
Jim Bethancourt
 
C Language fundamentals hhhhhhhhhhhh.ppt
C Language fundamentals hhhhhhhhhhhh.pptC Language fundamentals hhhhhhhhhhhh.ppt
C Language fundamentals hhhhhhhhhhhh.ppt
lalita57189
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8
Dian Aditya
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
Urs Peter
 
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
Sharon Rozinsky
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
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
 
[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
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
franciscoortin
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
Raffi Khatchadourian
 
C Language fundamentals hhhhhhhhhhhh.ppt
C Language fundamentals hhhhhhhhhhhh.pptC Language fundamentals hhhhhhhhhhhh.ppt
C Language fundamentals hhhhhhhhhhhh.ppt
lalita57189
 
Ad

Recently uploaded (20)

Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Exchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv SoftwareExchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv Software
Shoviv Software
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
Digital Twins Software Service in Belfast
Digital Twins Software Service in BelfastDigital Twins Software Service in Belfast
Digital Twins Software Service in Belfast
julia smits
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
Unit Two - Java Architecture and OOPS
Unit Two  -   Java Architecture and OOPSUnit Two  -   Java Architecture and OOPS
Unit Two - Java Architecture and OOPS
Nabin Dhakal
 
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdfTop Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
evrigsolution
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
Autodesk Inventor Crack (2025) Latest
Autodesk Inventor    Crack (2025) LatestAutodesk Inventor    Crack (2025) Latest
Autodesk Inventor Crack (2025) Latest
Google
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Adobe Media Encoder Crack FREE Download 2025
Adobe Media Encoder  Crack FREE Download 2025Adobe Media Encoder  Crack FREE Download 2025
Adobe Media Encoder Crack FREE Download 2025
zafranwaqar90
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509
Fermin Galan
 
How to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryErrorHow to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryError
Tier1 app
 
The Elixir Developer - All Things Open
The Elixir Developer - All Things OpenThe Elixir Developer - All Things Open
The Elixir Developer - All Things Open
Carlo Gilmar Padilla Santana
 
Solar-wind hybrid engery a system sustainable power
Solar-wind  hybrid engery a system sustainable powerSolar-wind  hybrid engery a system sustainable power
Solar-wind hybrid engery a system sustainable power
bhoomigowda12345
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint PresentationFrom Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
Shay Ginsbourg
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Exchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv SoftwareExchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv Software
Shoviv Software
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
Digital Twins Software Service in Belfast
Digital Twins Software Service in BelfastDigital Twins Software Service in Belfast
Digital Twins Software Service in Belfast
julia smits
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
Unit Two - Java Architecture and OOPS
Unit Two  -   Java Architecture and OOPSUnit Two  -   Java Architecture and OOPS
Unit Two - Java Architecture and OOPS
Nabin Dhakal
 
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdfTop Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
evrigsolution
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
Autodesk Inventor Crack (2025) Latest
Autodesk Inventor    Crack (2025) LatestAutodesk Inventor    Crack (2025) Latest
Autodesk Inventor Crack (2025) Latest
Google
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Adobe Media Encoder Crack FREE Download 2025
Adobe Media Encoder  Crack FREE Download 2025Adobe Media Encoder  Crack FREE Download 2025
Adobe Media Encoder Crack FREE Download 2025
zafranwaqar90
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509
Fermin Galan
 
How to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryErrorHow to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryError
Tier1 app
 
Solar-wind hybrid engery a system sustainable power
Solar-wind  hybrid engery a system sustainable powerSolar-wind  hybrid engery a system sustainable power
Solar-wind hybrid engery a system sustainable power
bhoomigowda12345
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint PresentationFrom Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
Shay Ginsbourg
 

Java8: Language Enhancements

  • 1. Java 8: Language enhancements Skelia
  • 2. Agenda  Lambda  Method References  Default methods  Static methods  Streams (bulk collection operations)
  • 3. Lambda - ? Expression describing the anonymous function, the result of the execution of which is the object of unknown origin, which implements the required functional interface
  • 4. Lambda. Examples  Java < 8 Comparator<Integer> cmp = new Comparator<Integer>() { @Override public int compare(Integer x, Integer y) { return (x < y) ? -1 : (x > y) ? 1 : 0; } };  Java 8 Comparator<Integer> cmp = (x, y) -> (x < y) ? -1 : (x > y) ? 1 : 0;
  • 5. Lambda. More examples  //interface Function<T, R> - R apply(T t); Function<String, Integer> f0 = (String s) -> Integer.parseInt(s);  //interface Supplier <T> - T get(); Supplier<Integer> answerFactory = () -> 42;  // interface Supplier<T> - T get(); Supplier<Integer> deepThought = () -> { long millis = TimeUnit.DAYS.toMillis(2737500000L); Thread.sleep(toThinkInMillis); return 42; };
  • 6. Method References Easy-to-read lambda expressions for methods that already have a name
  • 7. Method references. Precondition public class Member { public static int compareByAge(Member a, Member b) { return a.getBDay().compareTo(b.getBDay()); } } Member[] rosterAsArray = /* init array of Members */;
  • 8. Method references. Java < 8 class MemberAgeComparator implements Comparator<Member> { public int compare(Member a, Member b) { return Member.compareByAge(a, b); } } Arrays.sort(rosterAsArray, new MemberAgeComparator());
  • 9. Method references. Java 8 with Lambda Arrays.sort(rosterAsArray, (a, b) -> Member.compareByAge(a, b));
  • 11. Method References. Types Type Example Reference to a static method ContainingClass::staticMethodName Reference to an instance method of a particular object ContainingObject::instanceMethodNa me memberInstance::compareByName; (memberInstance.compareByName(a, b)) Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName String::compareToIgnoreCase (a.compareToIgnoreCase(b)) Reference to a constructor ClassName::new HashSet::new (() -> { return new HashSet<>(); })
  • 12. Default methods. The problem interface Collection<T> { /* @since 1.8 */ void removeAll(Predicate<T> p); }
  • 13. Default methods. Solution interface Collection<T> { /* @since 1.8 */ default void removeAll(Predicate<T> p) { // fallback implementation goes here } }
  • 15. Static methods public interface Ticket { String qDublin (); static Ticket random () { return () -> " toDublin "; } } assertEquals ("toDublin", Ticket.random().qDublin ());
  • 16. Streams. Problem public void printGroups(List<People> people) { Set<Group> groups = new HashSet<>(); for (Person p : people) { if (p.getAge() >= 65) groups.add(p.getGroup()); } List<Group> sorted = new ArrayList <>(groups); Collections.sort(sorted, new Comparator <Group>() { public int compare (Group a, Group b) { return Integer.compare(a.getSize(), b.getSize()); } }); for (Group g : sorted) System.out.println(g.getName()); }
  • 17. Streams. Solution public void printGroups (List<People> people) { people.stream() .filter(p -> p.getAge() > 65) .map(p -> p.getGroup()) .distinct() .sorted(comparing(g -> g.getSize()) .map(g -> g.getName()) .forEach(n -> System.out.println(n)); } Stream → sequence of elements.
  • 18. Streams. Design source op op … sink→ → → →  sources: collections, iterators, channels, ...  operations: filter, map, reduce, ...  sinks: collectors, forEach, iterator...
  • 19. Streams. Parallelism int v = list.parallelStream() .reduce(Math::max) .get ();  Implicit invocation of parallelStream() instead of stream()  Implementation is hidden  Uses ForkJoinPool
  • 20. Java 8 is coming... Scheduled to release in March 2014
  翻译: