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!

More Related Content

PPT
Java 8 Streams
PPTX
Java 8 Intro - Core Features
PDF
Java 8 lambda expressions
PDF
Java 8 Stream API. A different way to process collections.
PPTX
Introduction to java 8 stream api
PDF
Java 8 Lambda Built-in Functional Interfaces
PPTX
Chap2 class,objects contd
PDF
Scala is java8.next()
Java 8 Streams
Java 8 Intro - Core Features
Java 8 lambda expressions
Java 8 Stream API. A different way to process collections.
Introduction to java 8 stream api
Java 8 Lambda Built-in Functional Interfaces
Chap2 class,objects contd
Scala is java8.next()

What's hot (20)

PDF
Java 8 Lambda Expressions
PDF
Refactoring to Java 8 (Devoxx BE)
PDF
Pragmatic functional refactoring with java 8
PPTX
Java Generics
PDF
Introduction to JQ
PDF
Java 8: the good parts!
PPTX
Java generics
PDF
Implicit conversion and parameters
PDF
Writing beautiful code with Java 8
PDF
The... Wonderful? World of Lambdas
PPT
Java Generics for Dummies
PPT
Java operators
PPTX
Java gets a closure
DOCX
PPT
Scala functions
ODP
Functional Programming With Scala
PDF
jq: JSON - Like a Boss
PDF
Java 8 Workshop
PPTX
Java 8 Lambda Expressions
PDF
Lambda Expressions in Java
Java 8 Lambda Expressions
Refactoring to Java 8 (Devoxx BE)
Pragmatic functional refactoring with java 8
Java Generics
Introduction to JQ
Java 8: the good parts!
Java generics
Implicit conversion and parameters
Writing beautiful code with Java 8
The... Wonderful? World of Lambdas
Java Generics for Dummies
Java operators
Java gets a closure
Scala functions
Functional Programming With Scala
jq: JSON - Like a Boss
Java 8 Workshop
Java 8 Lambda Expressions
Lambda Expressions in Java
Ad

Viewers also liked (20)

ODP
Hello Java 8
PPTX
Java 8
PPTX
Lambda expressions java8 - yousry
PPTX
Java8 training - class 2
PDF
ODP
Java8
PDF
Какого сотрудника «оторвут с руками»
DOC
B nye online-coursesyllabus
PDF
分散システム読書会 13章-分散協調ベースシステム
ODP
Open geo day
PDF
PPT
African dwarf frogs
PPTX
Etologia
PPTX
AKU LULUSAN STATISTIKA DAN AKU BANGGA! Oleh Arsyil Hendra Saputra
KEY
Congratulations to you, mom!
PPT
B nye online-coursepp
PDF
分散システム読書会 06章-同期(後編)
PPTX
Java8 training - class 3
PDF
第4回 OSS運用管理勉強会(2014/02/04) 発表資料
Hello Java 8
Java 8
Lambda expressions java8 - yousry
Java8 training - class 2
Java8
Какого сотрудника «оторвут с руками»
B nye online-coursesyllabus
分散システム読書会 13章-分散協調ベースシステム
Open geo day
African dwarf frogs
Etologia
AKU LULUSAN STATISTIKA DAN AKU BANGGA! Oleh Arsyil Hendra Saputra
Congratulations to you, mom!
B nye online-coursepp
分散システム読書会 06章-同期(後編)
Java8 training - class 3
第4回 OSS運用管理勉強会(2014/02/04) 発表資料
Ad

Similar to Java8: Language Enhancements (20)

PPTX
java150929145120-lva1-app6892 (2).pptx
PPTX
Java 8 presentation
PPT
Major Java 8 features
PDF
Java 8 by example!
PPTX
Java 8 features
PPTX
Java 8 new features
PDF
Functional programming in Java 8 - workshop at flatMap Oslo 2014
PDF
Java 8 new features or the ones you might actually use
PDF
Wien15 java8
PPTX
PDF
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
PDF
Java 8
ODP
Introduction to Java 8
PDF
Java 8 best practices - Stephen Colebourne
PPTX
Java 8 Feature Preview
PPTX
Unit-3 Java New Features which is introduce in java 8.pptx
PPTX
Functional programming with_jdk8-s_ritter
PPTX
Lambdas and Laughs
PDF
PDF
Java SE 8 best practices
java150929145120-lva1-app6892 (2).pptx
Java 8 presentation
Major Java 8 features
Java 8 by example!
Java 8 features
Java 8 new features
Functional programming in Java 8 - workshop at flatMap Oslo 2014
Java 8 new features or the ones you might actually use
Wien15 java8
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
Java 8
Introduction to Java 8
Java 8 best practices - Stephen Colebourne
Java 8 Feature Preview
Unit-3 Java New Features which is introduce in java 8.pptx
Functional programming with_jdk8-s_ritter
Lambdas and Laughs
Java SE 8 best practices

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How to Confidently Manage Project Budgets
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
Presentation of Computer CLASS 2 .pptx
PDF
Become an Agentblazer Champion Challenge Kickoff
PPTX
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
System and Network Administraation Chapter 3
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Confidently Manage Project Budgets
PTS Company Brochure 2025 (1).pdf.......
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
ManageIQ - Sprint 268 Review - Slide Deck
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
ISO 45001 Occupational Health and Safety Management System
Presentation of Computer CLASS 2 .pptx
Become an Agentblazer Champion Challenge Kickoff
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
How to Migrate SBCGlobal Email to Yahoo Easily
A REACT POMODORO TIMER WEB APPLICATION.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
The Role of Automation and AI in EHS Management for Data Centers.pdf
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Understanding Forklifts - TECH EHS Solution
How to Choose the Right IT Partner for Your Business in Malaysia
VVF-Customer-Presentation2025-Ver1.9.pptx

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