SlideShare ist ein Scribd-Unternehmen logo
1 von 33
1CONFIDENTIAL
JAVA 8
2CONFIDENTIAL
‱ Java 8 General Overview
‱ Lambdas and Functional Interfaces
‱ Stream API
‱ Method References
‱ Interface’s Default and Static Methods
‱ Optional
‱ Date/Time API
‱ Nashorn, JS Engine
Agenda
3CONFIDENTIAL
Java installing problems
Please, pay attention when installing Java 8.
Note! Tomcat 7.0.x does not work with Java 8:
So make sure your JAVA_HOME is not pointing to Java 8 SDK directory or you will get
bunch of errors. Change your JAVA_HOME or configure your project properly.
4CONFIDENTIAL
Overview
JAVA 8 RELEASE (March 18, 2014)
Optional (value wrapper)Lambdas &
Method references
METHODS INSIDE JAVA INTERFACES
Default methods New Utilities
Stream API Date Time API Nashorn, JS Engine
LAMBDAS EXPRESSIONS
6CONFIDENTIAL
Lambdas (closures) syntax
Each lambda expression has two parts separated by an arrow token: the left hand side is our method
arguments, and the right hand side is what we do with those arguments (business logic)
7CONFIDENTIAL
Just compare
And it’s not even about lines of code
8CONFIDENTIAL
Motivation
‱ Shorter way of writing an implementation of a method for later execution.
‱ Provides simple horizontal solution that solves the "vertical problem" presented by inner classes.
‱ Lambda expressions are designed to allow code to be streamlined.
‱ Lambdas expressions do not produce any extra classes the way nested classes do.
9CONFIDENTIAL
Method reference
Oracle reference guide
“You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does
nothing but call an existing method. In those cases, it's often clearer to refer to the existing method by name.”
Kind Example
Reference to a static method ContainingClass::staticMethodName
Reference to an instance method of
a particular object
containingObject::instanceMethodName
Reference to an instance method of
an arbitrary object of a particular
type
ContainingType::methodName
Reference to a constructor ClassName::new
Kinds of methods referencesExample
10CONFIDENTIAL
Functional Interfaces
Conceptually, a functional interface has exactly one abstract method (e.g. Runnable, Callable, Comparator).
11CONFIDENTIAL
Pre-Built function library (java.util.function)
There are a lot of re-usable functional requirements that can be captured by functional interfaces and
lambdas. The designers of Java 8 have captured the common use cases and created a library of functions

 etc.
STREAMS API
13CONFIDENTIAL
Hello, Streams
Streams let you process data in a declarative way. We do not say how to process the data, we
just say what to do with the data.
Let’s say we need to find all transactions of type ‘grocery’ and return a list of transaction
IDs sorted in decreasing order of transaction value.
14CONFIDENTIAL
java.util.stream.Stream: feel the power.
It takes 10 seconds to process 10 filesIt takes 1 second to process 10 files
15CONFIDENTIAL
Streams versus Collections
Q. What is the difference?
Q. So when should I use streams and why?
Q. On demand?
A. In a nutshell, collections are about data and streams are about computations.
A. Use streams when things are computed. A collection is an in-memory data structure, which holds all the
values that the data structure currently has—every element in the collection has to be computed before it can
be added to the collection. In contrast, a stream is a conceptually fixed data structure in which elements are
computed on demand.
A. Yes, look:
limit(2) uses short-
circuiting;
we need to process
only part of the
stream, not all of
it.
16CONFIDENTIAL
So

Stream as an abstraction for expressing
efficient, SQL-like operations on a
collection of data.
The Streams API will internally
decompose your query to leverage the
multiple cores on your computer.
OPTIONAL
18CONFIDENTIAL
Optional
A. “
 a container object which may or may not contain a non-null value...” Java 8 Reference Guide
Q. Of course, the null reference is the source of many problems. But how does Optional solve them?
Q. What is it?
Q. Why do we need them?
A. Because "Null sucks." Doug Le (a professor of computer science)
A. Enough talking; let's see some code!
19CONFIDENTIAL
java.util.Optional usage
Do Something If a Value Is Present
Default Values and Actions
Creating of Optional
Old style
New style
Old style
New style
Rejecting Certain Values Using the filter Method
Old style
New style
METHODSINSIDE JAVA INTERFACES
21CONFIDENTIAL
What are default methods?
Default methods enable you to add new functionality to the interfaces of your libraries and ensure
backward compatibility with code written for older versions of those interfaces.
The forEach isn’t declared by java.util.List nor the java.util.Collection interface yet. The solution would
be to just add the new method to the existing interface and provide the implementation where required in
the JDK. But, once published, it is impossible to add methods to an interface without breaking the existing
implementation
Syntax
22CONFIDENTIAL
Extending interfaces with default methods
When you extend an interface that contains a default method, you can do the following:
 Not mention the default method at all, which lets inherit the default method.
 Redeclare the default method, which makes it abstract.
 Redefine the default method, which overrides it.
23CONFIDENTIAL
Tricks
“What if the class implements two interfaces and both those interfaces define a default method with
the same signature?”
24CONFIDENTIAL
Static methods
Like static methods in classes, you specify that a method definition in an interface is a static method
with the static keyword at the beginning of the method signature. All method declarations in an
interface, including static methods, are implicitly public, so you can omit the public modifier.
Applications. Examples
The Comparator interface has been enhanced with this ability with the default method reversed:
DATE API
26CONFIDENTIAL
Date API. Core ideas.
The new API is driven by three core ideas:
 Domain-driven design. The new API models its domain very precisely with classes that represent
different use cases for Date and Time closely. This differs from previous Java libraries that were quite
poor in that regard.
 Immutable-value classes. One of the serious weaknesses of the existing formatters in Java is that they aren’t
thread-safe. This puts the burden on developers to use them in a thread-safe manner and to think about
concurrency problems in their day-to-day development of date-handling code. The new API avoids this issue
by ensuring that all its core classes are immutable and represent well-defined values.
 Separation of chronologies. The new API allows people to work with different calendaring systems in order to
support the needs of users in some areas of the world, such as Japan or Thailand, that don’t necessarily follow
ISO-8601. It does so without imposing additional burden on the majority of developers, who need to work only
with the standard chronology
27CONFIDENTIAL
Date API. Creating.
The first classes you will probably encounter when using the new API are LocalDate and LocalTime.
Standard Java getter conventions are used in order to obtain values from Java SE 8 classes
28CONFIDENTIAL
Date API. Creating (2).
There are also methods for calculations based on the different fields.
The new API also has the concept of an adjuster— a block of code that can be used to wrap
up common processing logic
29CONFIDENTIAL
Date API. Go ahead.
The new API supports different precision time points by offering types to represent a date, a
time, and date with time, but obviously there are notions of precision that are more fine-
grained than this.
Time Zones
A time zone is a set of rules, corresponding to a region in which the standard time is the same
NASHORN. JS ENGINE
31CONFIDENTIAL
Nashorn. JS Engine.
Nashorn's goal is to implement a lightweight high-performance JavaScript runtime in Java with a
native JVM.
MyScript.js
32CONFIDENTIAL
?questions and answers
33CONFIDENTIAL
http://docs.oracle.com/Follow to get more

Weitere Àhnliche Inhalte

Was ist angesagt?

Collections In Java
Collections In JavaCollections In Java
Collections In Java
Binoj T E
 

Was ist angesagt? (20)

Java 8 Default Methods
Java 8 Default MethodsJava 8 Default Methods
Java 8 Default Methods
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
Java Lambda Expressions.pptx
Java Lambda Expressions.pptxJava Lambda Expressions.pptx
Java Lambda Expressions.pptx
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
07 java collection
07 java collection07 java collection
07 java collection
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
 
Java collections
Java collectionsJava collections
Java collections
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Spring Core
Spring CoreSpring Core
Spring Core
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of ControlJava Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 

Andere mochten auch

Andere mochten auch (7)

New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
55 New Features in Java SE 8
55 New Features in Java SE 855 New Features in Java SE 8
55 New Features in Java SE 8
 
Cracking OCA and OCP Java 8 Exams
Cracking OCA and OCP Java 8 ExamsCracking OCA and OCP Java 8 Exams
Cracking OCA and OCP Java 8 Exams
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
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
 
Supercharged java 8 : with cyclops-react
Supercharged java 8 : with cyclops-reactSupercharged java 8 : with cyclops-react
Supercharged java 8 : with cyclops-react
 
Sailing with Java 8 Streams
Sailing with Java 8 StreamsSailing with Java 8 Streams
Sailing with Java 8 Streams
 

Ähnlich wie Java 8 - Features Overview

Designing Better API
Designing Better APIDesigning Better API
Designing Better API
Kaniska Mandal
 
Java 7 & 8
Java 7 & 8Java 7 & 8
Java 7 & 8
Ken Coenen
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
New York City College of Technology Computer Systems Technology Colloquium
 
Java 7 & 8 - A&BP CC
Java 7 & 8 - A&BP CCJava 7 & 8 - A&BP CC
Java 7 & 8 - A&BP CC
JWORKS powered by Ordina
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
Nayden Gochev
 

Ähnlich wie Java 8 - Features Overview (20)

Insight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FPInsight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FP
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
Designing Better API
Designing Better APIDesigning Better API
Designing Better API
 
Java 8
Java 8Java 8
Java 8
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 
Multi-dimensional exploration of API usage - ICPC13 - 21-05-13
Multi-dimensional exploration of API usage - ICPC13 - 21-05-13Multi-dimensional exploration of API usage - ICPC13 - 21-05-13
Multi-dimensional exploration of API usage - ICPC13 - 21-05-13
 
Smart Migration to JDK 8
Smart Migration to JDK 8Smart Migration to JDK 8
Smart Migration to JDK 8
 
Java 7 & 8
Java 7 & 8Java 7 & 8
Java 7 & 8
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
 
Java 8-revealed
Java 8-revealedJava 8-revealed
Java 8-revealed
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
Java 8
Java 8Java 8
Java 8
 
Java 7 & 8 - A&BP CC
Java 7 & 8 - A&BP CCJava 7 & 8 - A&BP CC
Java 7 & 8 - A&BP CC
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to Interfaces
 
Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2
 
Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
 
ICON UK '13 - Apache Software: The FREE Java toolbox you didn't know you had !!
ICON UK '13 - Apache Software: The FREE Java toolbox you didn't know you had !!ICON UK '13 - Apache Software: The FREE Java toolbox you didn't know you had !!
ICON UK '13 - Apache Software: The FREE Java toolbox you didn't know you had !!
 

KĂŒrzlich hochgeladen

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

KĂŒrzlich hochgeladen (20)

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
TỔNG ÔN TáșŹP THI VÀO LỚP 10 MÔN TIáșŸNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGở Â...
TỔNG ÔN TáșŹP THI VÀO LỚP 10 MÔN TIáșŸNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGở Â...TỔNG ÔN TáșŹP THI VÀO LỚP 10 MÔN TIáșŸNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGở Â...
TỔNG ÔN TáșŹP THI VÀO LỚP 10 MÔN TIáșŸNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGở Â...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

Java 8 - Features Overview

  • 2. 2CONFIDENTIAL ‱ Java 8 General Overview ‱ Lambdas and Functional Interfaces ‱ Stream API ‱ Method References ‱ Interface’s Default and Static Methods ‱ Optional ‱ Date/Time API ‱ Nashorn, JS Engine Agenda
  • 3. 3CONFIDENTIAL Java installing problems Please, pay attention when installing Java 8. Note! Tomcat 7.0.x does not work with Java 8: So make sure your JAVA_HOME is not pointing to Java 8 SDK directory or you will get bunch of errors. Change your JAVA_HOME or configure your project properly.
  • 4. 4CONFIDENTIAL Overview JAVA 8 RELEASE (March 18, 2014) Optional (value wrapper)Lambdas & Method references METHODS INSIDE JAVA INTERFACES Default methods New Utilities Stream API Date Time API Nashorn, JS Engine
  • 6. 6CONFIDENTIAL Lambdas (closures) syntax Each lambda expression has two parts separated by an arrow token: the left hand side is our method arguments, and the right hand side is what we do with those arguments (business logic)
  • 7. 7CONFIDENTIAL Just compare And it’s not even about lines of code
  • 8. 8CONFIDENTIAL Motivation ‱ Shorter way of writing an implementation of a method for later execution. ‱ Provides simple horizontal solution that solves the "vertical problem" presented by inner classes. ‱ Lambda expressions are designed to allow code to be streamlined. ‱ Lambdas expressions do not produce any extra classes the way nested classes do.
  • 9. 9CONFIDENTIAL Method reference Oracle reference guide “You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it's often clearer to refer to the existing method by name.” Kind Example Reference to a static method ContainingClass::staticMethodName Reference to an instance method of a particular object containingObject::instanceMethodName Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName Reference to a constructor ClassName::new Kinds of methods referencesExample
  • 10. 10CONFIDENTIAL Functional Interfaces Conceptually, a functional interface has exactly one abstract method (e.g. Runnable, Callable, Comparator).
  • 11. 11CONFIDENTIAL Pre-Built function library (java.util.function) There are a lot of re-usable functional requirements that can be captured by functional interfaces and lambdas. The designers of Java 8 have captured the common use cases and created a library of functions 
 etc.
  • 13. 13CONFIDENTIAL Hello, Streams Streams let you process data in a declarative way. We do not say how to process the data, we just say what to do with the data. Let’s say we need to find all transactions of type ‘grocery’ and return a list of transaction IDs sorted in decreasing order of transaction value.
  • 14. 14CONFIDENTIAL java.util.stream.Stream: feel the power. It takes 10 seconds to process 10 filesIt takes 1 second to process 10 files
  • 15. 15CONFIDENTIAL Streams versus Collections Q. What is the difference? Q. So when should I use streams and why? Q. On demand? A. In a nutshell, collections are about data and streams are about computations. A. Use streams when things are computed. A collection is an in-memory data structure, which holds all the values that the data structure currently has—every element in the collection has to be computed before it can be added to the collection. In contrast, a stream is a conceptually fixed data structure in which elements are computed on demand. A. Yes, look: limit(2) uses short- circuiting; we need to process only part of the stream, not all of it.
  • 16. 16CONFIDENTIAL So
 Stream as an abstraction for expressing efficient, SQL-like operations on a collection of data. The Streams API will internally decompose your query to leverage the multiple cores on your computer.
  • 18. 18CONFIDENTIAL Optional A. “
 a container object which may or may not contain a non-null value...” Java 8 Reference Guide Q. Of course, the null reference is the source of many problems. But how does Optional solve them? Q. What is it? Q. Why do we need them? A. Because "Null sucks." Doug Le (a professor of computer science) A. Enough talking; let's see some code!
  • 19. 19CONFIDENTIAL java.util.Optional usage Do Something If a Value Is Present Default Values and Actions Creating of Optional Old style New style Old style New style Rejecting Certain Values Using the filter Method Old style New style
  • 21. 21CONFIDENTIAL What are default methods? Default methods enable you to add new functionality to the interfaces of your libraries and ensure backward compatibility with code written for older versions of those interfaces. The forEach isn’t declared by java.util.List nor the java.util.Collection interface yet. The solution would be to just add the new method to the existing interface and provide the implementation where required in the JDK. But, once published, it is impossible to add methods to an interface without breaking the existing implementation Syntax
  • 22. 22CONFIDENTIAL Extending interfaces with default methods When you extend an interface that contains a default method, you can do the following:  Not mention the default method at all, which lets inherit the default method.  Redeclare the default method, which makes it abstract.  Redefine the default method, which overrides it.
  • 23. 23CONFIDENTIAL Tricks “What if the class implements two interfaces and both those interfaces define a default method with the same signature?”
  • 24. 24CONFIDENTIAL Static methods Like static methods in classes, you specify that a method definition in an interface is a static method with the static keyword at the beginning of the method signature. All method declarations in an interface, including static methods, are implicitly public, so you can omit the public modifier. Applications. Examples The Comparator interface has been enhanced with this ability with the default method reversed:
  • 26. 26CONFIDENTIAL Date API. Core ideas. The new API is driven by three core ideas:  Domain-driven design. The new API models its domain very precisely with classes that represent different use cases for Date and Time closely. This differs from previous Java libraries that were quite poor in that regard.  Immutable-value classes. One of the serious weaknesses of the existing formatters in Java is that they aren’t thread-safe. This puts the burden on developers to use them in a thread-safe manner and to think about concurrency problems in their day-to-day development of date-handling code. The new API avoids this issue by ensuring that all its core classes are immutable and represent well-defined values.  Separation of chronologies. The new API allows people to work with different calendaring systems in order to support the needs of users in some areas of the world, such as Japan or Thailand, that don’t necessarily follow ISO-8601. It does so without imposing additional burden on the majority of developers, who need to work only with the standard chronology
  • 27. 27CONFIDENTIAL Date API. Creating. The first classes you will probably encounter when using the new API are LocalDate and LocalTime. Standard Java getter conventions are used in order to obtain values from Java SE 8 classes
  • 28. 28CONFIDENTIAL Date API. Creating (2). There are also methods for calculations based on the different fields. The new API also has the concept of an adjuster— a block of code that can be used to wrap up common processing logic
  • 29. 29CONFIDENTIAL Date API. Go ahead. The new API supports different precision time points by offering types to represent a date, a time, and date with time, but obviously there are notions of precision that are more fine- grained than this. Time Zones A time zone is a set of rules, corresponding to a region in which the standard time is the same
  • 31. 31CONFIDENTIAL Nashorn. JS Engine. Nashorn's goal is to implement a lightweight high-performance JavaScript runtime in Java with a native JVM. MyScript.js

Hinweis der Redaktion

  1. Java 8 by Sergii Stets
  2. Agenda
  3. Before starting I want pay your attention to some problems you might have when installing new Java 8 SDK. If you use any Tomcat of version 7 on your local machine you will probably fail to run it on top of Java 8. Because Apache Tomcat 7 is not compatible with Java 8. So there are several options: you have to update your Tomcat. Or if you do not want any updates, make sure your JAVA_HOME is not pointing to Java 8 SDK directory.
  4. So the first Java 8 release was at the 18-th of March in 2014. Later on Oracle released different security updates, different patches and so on. The main features of Java 8 are: Lambda expression - Adds functional processing capability to JAVA. Optional - Emphasis on best practices, to handle null values properly. Default method - Interface to have default method implementation. New Tools - New compiler tools and utilities are added like jdeps to figure out the dependencies. Stream API - New stream API to facilitate pipeline processing. Date Time API - Improved date time api. Nashorn , JavaScript Engine - A JAVA based engine to execute JavaScript code.
  5. This is Duke 
  6. Each lambda expression has two parts separated by an arrow token: the left hand side is our method arguments, and the right hand side is what we do with those arguments (business logic) Let’s take a look at the example below. 1 2 as you may have noticed we omitted types of the arguments and that OK. You will understand it later. 3 If your business logic is much more complex than just a line of code you may use curly braces. 4 no args
  7. And it’s not even about lines of code. Lambdas expressions do not produce any extra classes the way inner classes do – so you don’t overheat JVM with a lot of class files it should load into memory. Lambdas has some low-level implementation. It is not just a syntax sugar over nested classes.
  8. Useful info: When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions As mentioned in the section Nested Classes, nested classes enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code. Local classes, anonymous classes, and lambda expressions also impart these advantages; however, they are intended to be used for more specific situations: Local class: Use it if you need to create more than one instance of a class, access its constructor, or introduce a new, named type (because, for example, you need to invoke additional methods later). Anonymous class: Use it if you need to declare fields or additional methods. Lambda expression: Use it if you are encapsulating a single unit of behavior that you want to pass to other code. For example, you would use a lambda expression if you want a certain action performed on each element of a collection, when a process is completed, or when a process encounters an error. Use it if you need a simple instance of a functional interface and none of the preceding criteria apply (for example, you do not need a constructor, a named type, fields, or additional methods). Nested class: Use it if your requirements are similar to those of a local class, you want to make the type more widely available, and you don't require access to local variables or method parameters. Use a non-static nested class (or inner class) if you require access to an enclosing instance's non-public fields and methods. Use a static nested class if you don't require this access.
  9. You use lambda expressions to create anonymous methods. Sometimes a lambda expression does nothing but call an existing method. In those cases, it's often clearer to refer to the existing method by name. So Oracle has just created a so called syntax sugar. There different kinds of method references

  10. Lambdas expressed single method classes. They can either be assigned to a variable or passed around to other methods just like we pass data as arguments. And here where functional interfaces come into. Conceptually, a functional interface has exactly one abstract method. Well know classes: (e.g. Runnable, Callable, Comparator). In Java 8 all the mentioned classes are marked with @FunctionalInterface annotation. It just a marker you may not use it. An informative annotation used to indicate that an interface is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method 1 We tell the compiler ‘I want to implement check method from ITrade so to someone else can pass me as arguments to another method’ 2 The second one is the same. 3 We do not change the signature of our method, we just pass different trend lambdas http://radar.oreilly.com/2014/08/java-8-functional-interfaces.html
  11. http://radar.oreilly.com/2014/08/java-8-functional-interfaces.html
  12. http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html Streams let you process data in a declarative way. We do not say how to process the data, we just say what to do with the data. Example 
 By the way Stream operations that can be connected are called intermediate operations. 
  13. Power of parallel streams
  14. 
 (in the end) This is because limit(2) uses short-circuiting; we need to process only part of the stream, not all of it, to return a result. List<String> phrases = Arrays.asList( "sporadic perjury", "confounded skimming", "incumbent jailer", "confounded jailer"); List<String> uniqueWords = phrases .stream() .flatMap(phrase -> Stream.of(phrase.split(" +"))) .distinct() .sorted() .collect(Collectors.toList()); System.out.println("Unique words: " + uniqueWords);... and the output: Unique words: [confounded, incumbent, jailer, perjury, skimming, sporadic]
  15. http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html
  16. http://geekabyte.blogspot.co.il/2015/01/using-optional-effectively-in-java-8.html http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html You use orElse() in situation where you want to have an alternative value in case T is null. orElseGet() method is similar to orElse(), but instead of providing an alternative, should in case null is encountered, you provide code which can be used to generate the alternative value . orElseThrow() comes in handy when you want to decide which exception to throw in case a null value is encountered. 
  17. Default methods
  18. http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/ http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
  19. Just read! http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/ http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
  20. The primary goal of default methods is to enable an evolution of standard JDK interfaces and provide a smooth experience when we finally start using lambdas in Java 8. Who knows, maybe in the future we will see some more applications of default methods for API design.
  21. This example demonstrates how the Comparator interface has been enhanced with default methods, static methods, lambda expressions, and method references to create more expressive library methods whose functionality programmers can quickly deduce by looking at how they are invoked. Use these constructs to enhance the interfaces in your libraries.
  22. http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html
  23. http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html
  24. http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html
  25. http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html
  26. Nashorn
  27. http://openjdk.java.net/projects/nashorn/ http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html Nashorn's goal is to implement a lightweight high-performance JavaScript runtime in Java with a native JVM. 
  28. If you have any questions, please do not hesitate to ask me. I will send you homework later.