SlideShare a Scribd company logo
10+ new features
you ought to know
using Java 8
Oleg Tsal-Tsalko
JavaDay Lviv 2015
LEAD SOFTWARE ENGINEER AT EPAM SYSTEMS.
PATIONATE DEVELOPER, SPEAKER, ACTIVE MEMBER OF
KIEV JUG.
PARTICIPATE IN DIFFERENT EDUCATIONAL INITIATIVES,
ENGINEERING EVENTS AND JCP/ADOPTJSR PROGRAMS.
OLEG TSAL-TSALKO
ABOUT ME
3CONFIDENTIAL
•  Stream API
•  Lambdas and method references
•  Default methods
•  Optional values
•  Date & Time API
•  Stamped Locks and Concurrent Adders
•  Type annotations and repeated annotations
•  New files operations
•  Overflow operations and Base64 encoding
•  Nashorn JavaScript engine
Agenda
4CONFIDENTIAL
•  An abstraction that supports bulk operations
over sequence of elements
•  Not a data structure
•  Simple and logical chaining of operations
•  Lazy evaluation
•  Internal parallelism if required
•  Concept of functional programming in Java
#1 Stream API
5CONFIDENTIAL
•  Collection.stream()
•  IntStream.range()
•  Stream.of()
•  Arrays.stream()
•  BufferedReader.lines()
•  CharSequence.chars()
•  Pattern.splitAsStream()
How can I get a Stream?
6CONFIDENTIAL
Fluent and simple
txns.stream()
.filter(t ->t.getBuyer().getAge()>=65)
.map(Txn::getSeller)
.distinct()
.sort(comparing(Seller::getName))
.forEach(s -> System.out.println(s.getName());
7CONFIDENTIAL
java.util.function Package:
Predicate<T>
Determine if the input of type T matches some criteria
Consumer<T>
Accept a single input argument of type T, and return no result
Function<T, R>
Apply a function to the input type T, generating a result of type R
java.util.stream Package:
Collector<T, A, R>
Used to collect stream of elements of type T into single result of
type R
Collectors
Contains number of predefined Collectors
Functional interfaces
8CONFIDENTIAL
A lambda expression is an anonymous function
–  Argument list, a return type, and a body
(Object o) -> o.toString()
–  Method reference
Object::toString
–  Capture value from enclosing context
(Person p) ->p.getName().equals(name)
–  Type inference
p -> p.getName().equals(name)
–  Forget to use anonymous classes
You can now pass behavior as a data between your objects
#2 Lambdas
9CONFIDENTIAL
#3 Method reference
FileFilter x = (File f) -> f.canRead();
FileFilter x = File::canRead;
FileFilter x = new FileFilter() {
public boolean accept(File f) {
return f.canRead();
}
};
10CONFIDENTIAL
Since Java doesn’t have Function type on it’s own
Lambda’s type evaluated to some Functional
interface:
Runnable r = () -> { doSmth(); };
Callable c = () -> calcResult();
Comparator comp = (o1, o2) -> compare(o1, o2);
What’s your type?
11CONFIDENTIAL
Influence on existing classes
java.lang.Iterable#forEach()
java.util.Collection#stream()
java.util.Collection#parallelStream()
java.util.Comparator#comparing(…)
java.util.Comparator#thenComparing(…)
java.util.Comparator#reverse()
…
12CONFIDENTIAL
SHOW ME THE CODE
Lambdas and streams
TALK IS CHEAP
13CONFIDENTIAL
•  Libraries need to evolve, or they stagnate
•  Java preserves compatibility
•  Multiple inheritance in Java?
•  Default methods can reduce implementation
burden
•  Resolution rules:
– Class wins over interface
– More specific subclass wins
– No 3rd rule
#4 Default methods
14CONFIDENTIAL
•  Annoyed by NPEs?
•  Should your method return null or throw
RuntimeException?
•  Wondering how to implement SpecialCase
pattern?
•  Inspired by functional programming and want to
build a method chain for NULL handling?
=> You are lucky with Java 8…
#5 Optional
15CONFIDENTIAL
#6 Date & Time API
•  Replaces old ambiguous java.util.Date,
Calendar, TimeZone, DateFormat classes
with lots of deprecations
•  More fluent/simple/clean API
•  Immutable classes
•  Using Java8 features including lambdas
•  Precise separation of concepts
16CONFIDENTIAL
•  LocalDate – a date only
•  LocalTime – a time only
•  LocalDateTime – date with time
•  ZonedDateTime – date with time in time zone
•  Period - date-based amount of time
•  Duration - time-based amount of time
•  And more…
Range of types
17CONFIDENTIAL
Time Zones
If you can avoid using TimeZones – do it!
If not – use ZonedDateAndTime!
18CONFIDENTIAL
•  ZoneId – replacement for TimeZone class (e.g.
“Europe/London”, “Europe/Kiev”)
•  ZoneOffset – representing offset from UTC time
•  ZoneRules – behind the scenes class which
defines time zone rules
•  ZonedDateTime – main date/time class which is
aware of time zones
TimeZone classes in Java 8
19CONFIDENTIAL
New API is very flexible because it based on number of
abstractions at it’s bottom:
Temporal – parent class for all date/time objects which
defines mutation operation for them such as plus/minus/
with
TemporalAdjuster – functional interface which responsible
for mutating Temporal objects
TemporalField – represents parts/fields of date/time
objects such as (DAY_OF_WEEK, MONTH, etc.)
TemporalUnit – represents type of date/time values such as
(MINUTES, DAYS, YEARS, etc.)
TemporalAmount – class which represents amount of time
Power of abstraction
20CONFIDENTIAL
SHOW ME THE CODE
Optional and Date and Time API
TALK IS CHEAP
21CONFIDENTIAL
•  Whenever we think of using RWLock
•  RWLock can cause starvation of readers
•  RWLock is pessimistic on reads
•  Read lock is optimistic
•  Optimistic lock can be converted to pessimistic
lock if necessary
•  Write locks are always pessimistic
#7 StampedLock
22CONFIDENTIAL
•  [Java 5] When readers are given priority,
then writers might never be able to
complete
•  [Java 6] But when writers are given
priority, readers might be starved
⇒ In Java 8 use StampedLock instead!
RWLock starvation
23CONFIDENTIAL
•  Pros
– Has much better performance than
ReentrantReadWriteLock
– Latest versions do not suffer from starvation
of writers
•  Cons
– Idioms are more difficult to get right than
with ReadWriteLock
– A small difference can make a big difference
in performance
StampedLock
24CONFIDENTIAL
How would you implement counter in
multithreaded environment?
•  Dirty counters (silly)
•  Synchronized (very slow)
•  RWLock (slow)
•  Volatile (only if you have 1 writer/updater)
•  AtomicInteger (yes, before Java 8…)
⇒  LongAdder in Java 8!
#8 Concurrent Adders
25CONFIDENTIAL
WANT MORE?
26CONFIDENTIAL
9 Improved annotations
10 New file operations
11 Overflow operations
12 Base64 encoding
13 Nashorn JavaScript engine
27CONFIDENTIAL
•  Process Termination
•  Strong Random Generation
•  Date.toInstant()
•  Interface static methods
•  Better Type Inference
•  Parameter names support by compiler
•  Parallel Arrays
•  Nashorn CLI JavaScript interpriter
•  Class dependency analyzer
However there are even more…
28CONFIDENTIAL
•  https://www.google.com/?gws_rd=ssl
•  https://github.com/RichardWarburton/java-8-
lambdas-exercises
•  http://javaspecialists.eu/talks/jfokus13/
PhaserAndStampedLock.pdf
•  http://www.javacodegeeks.com/2014/05/
java-8-features-tutorial.html
•  http://blog.takipi.com/10-features-in-java-8-
you-havent-heard-of/
Resources
29CONFIDENTIAL
QUESTIONS?
Skype: oleg.tsalko
Twitter: @tsaltsol
THANK YOU
Ad

More Related Content

What's hot (20)

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
Simon Ritter
 
Java SE 8 library design
Java SE 8 library designJava SE 8 library design
Java SE 8 library design
Stephen Colebourne
 
The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8
Takipi
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
NewCircle Training
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
Manav Prasad
 
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 Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
icarter09
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
Sanjoy Kumar Roy
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
NexThoughts Technologies
 
Java SE 8
Java SE 8Java SE 8
Java SE 8
Simon Ritter
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7
Deniz Oguz
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
LivePerson
 
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
 
Whats New in Java 5, 6, & 7 (Webinar Presentation - June 2013)
Whats New in Java 5, 6, & 7 (Webinar Presentation - June 2013)Whats New in Java 5, 6, & 7 (Webinar Presentation - June 2013)
Whats New in Java 5, 6, & 7 (Webinar Presentation - June 2013)
DevelopIntelligence
 
Smart Migration to JDK 8
Smart Migration to JDK 8Smart Migration to JDK 8
Smart Migration to JDK 8
Geertjan Wielenga
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
Erhan Bagdemir
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
Rahman USTA
 
Java 8
Java 8Java 8
Java 8
Sudipta K Paik
 
Refactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 RecapRefactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 Recap
Dave Orme
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
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
Simon Ritter
 
The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8
Takipi
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
NewCircle Training
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
icarter09
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7
Deniz Oguz
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
LivePerson
 
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
 
Whats New in Java 5, 6, & 7 (Webinar Presentation - June 2013)
Whats New in Java 5, 6, & 7 (Webinar Presentation - June 2013)Whats New in Java 5, 6, & 7 (Webinar Presentation - June 2013)
Whats New in Java 5, 6, & 7 (Webinar Presentation - June 2013)
DevelopIntelligence
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
Erhan Bagdemir
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
Rahman USTA
 
Refactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 RecapRefactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 Recap
Dave Orme
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 

Similar to Java 8 features (20)

Java Closures
Java ClosuresJava Closures
Java Closures
Ben Evans
 
Scala-Ls1
Scala-Ls1Scala-Ls1
Scala-Ls1
Aniket Joshi
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
Viplav Jain
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
fanf42
 
The Rise of Functional Programming
The Rise of Functional ProgrammingThe Rise of Functional Programming
The Rise of Functional Programming
Tjerk W
 
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGNIntroducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
Manish Pandit
 
Scala Programming Introduction
Scala Programming IntroductionScala Programming Introduction
Scala Programming Introduction
airisData
 
Clojure in real life 17.10.2014
Clojure in real life 17.10.2014Clojure in real life 17.10.2014
Clojure in real life 17.10.2014
Metosin Oy
 
Typesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and PlayTypesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and Play
Luka Zakrajšek
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Mohammad Hossein Rimaz
 
JSR 335 / java 8 - update reference
JSR 335 / java 8 - update referenceJSR 335 / java 8 - update reference
JSR 335 / java 8 - update reference
sandeepji_choudhary
 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdUnit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rd
prat0ham
 
Spark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin OderskySpark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin Odersky
Spark Summit
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and Xitrum
Ngoc Dao
 
JAVA 8 Parallel Stream
JAVA 8 Parallel StreamJAVA 8 Parallel Stream
JAVA 8 Parallel Stream
Tengwen Wang
 
What’s new in java 8
What’s new in java 8What’s new in java 8
What’s new in java 8
Rajmahendra Hegde
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usage
AsmaShaikh478737
 
Introduction to Scala language
Introduction to Scala languageIntroduction to Scala language
Introduction to Scala language
Aaqib Pervaiz
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
i i
 
Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)
Martijn Verburg
 
Java Closures
Java ClosuresJava Closures
Java Closures
Ben Evans
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
Viplav Jain
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
fanf42
 
The Rise of Functional Programming
The Rise of Functional ProgrammingThe Rise of Functional Programming
The Rise of Functional Programming
Tjerk W
 
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGNIntroducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
Manish Pandit
 
Scala Programming Introduction
Scala Programming IntroductionScala Programming Introduction
Scala Programming Introduction
airisData
 
Clojure in real life 17.10.2014
Clojure in real life 17.10.2014Clojure in real life 17.10.2014
Clojure in real life 17.10.2014
Metosin Oy
 
Typesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and PlayTypesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and Play
Luka Zakrajšek
 
JSR 335 / java 8 - update reference
JSR 335 / java 8 - update referenceJSR 335 / java 8 - update reference
JSR 335 / java 8 - update reference
sandeepji_choudhary
 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdUnit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rd
prat0ham
 
Spark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin OderskySpark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin Odersky
Spark Summit
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and Xitrum
Ngoc Dao
 
JAVA 8 Parallel Stream
JAVA 8 Parallel StreamJAVA 8 Parallel Stream
JAVA 8 Parallel Stream
Tengwen Wang
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usage
AsmaShaikh478737
 
Introduction to Scala language
Introduction to Scala languageIntroduction to Scala language
Introduction to Scala language
Aaqib Pervaiz
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
i i
 
Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)
Martijn Verburg
 
Ad

More from Oleg Tsal-Tsalko (14)

Developer on a mission (Devoxx UA 2021)
Developer on a mission (Devoxx UA 2021)Developer on a mission (Devoxx UA 2021)
Developer on a mission (Devoxx UA 2021)
Oleg Tsal-Tsalko
 
Developer on a mission
Developer on a missionDeveloper on a mission
Developer on a mission
Oleg Tsal-Tsalko
 
From Streams to Reactive Streams
From Streams to Reactive StreamsFrom Streams to Reactive Streams
From Streams to Reactive Streams
Oleg Tsal-Tsalko
 
Java 9 Jigsaw HackDay
Java 9 Jigsaw HackDayJava 9 Jigsaw HackDay
Java 9 Jigsaw HackDay
Oleg Tsal-Tsalko
 
JUG UA AdoptJSR participation
JUG UA AdoptJSR participationJUG UA AdoptJSR participation
JUG UA AdoptJSR participation
Oleg Tsal-Tsalko
 
Develop modern apps using Spring ecosystem at time of BigData
Develop modern apps using Spring ecosystem at time of BigData Develop modern apps using Spring ecosystem at time of BigData
Develop modern apps using Spring ecosystem at time of BigData
Oleg Tsal-Tsalko
 
Lambdas HOL
Lambdas HOLLambdas HOL
Lambdas HOL
Oleg Tsal-Tsalko
 
Java 8 date & time javaday2014
Java 8 date & time javaday2014Java 8 date & time javaday2014
Java 8 date & time javaday2014
Oleg Tsal-Tsalko
 
Java 8 date & time
Java 8 date & timeJava 8 date & time
Java 8 date & time
Oleg Tsal-Tsalko
 
Get ready for spring 4
Get ready for spring 4Get ready for spring 4
Get ready for spring 4
Oleg Tsal-Tsalko
 
Enterprise Integration Patterns
Enterprise Integration PatternsEnterprise Integration Patterns
Enterprise Integration Patterns
Oleg Tsal-Tsalko
 
Distributed systems and scalability rules
Distributed systems and scalability rulesDistributed systems and scalability rules
Distributed systems and scalability rules
Oleg Tsal-Tsalko
 
Next stop: Spring 4
Next stop: Spring 4Next stop: Spring 4
Next stop: Spring 4
Oleg Tsal-Tsalko
 
JUG involvment in JCP and AdopJSR program
JUG involvment in JCP and AdopJSR programJUG involvment in JCP and AdopJSR program
JUG involvment in JCP and AdopJSR program
Oleg Tsal-Tsalko
 
Developer on a mission (Devoxx UA 2021)
Developer on a mission (Devoxx UA 2021)Developer on a mission (Devoxx UA 2021)
Developer on a mission (Devoxx UA 2021)
Oleg Tsal-Tsalko
 
From Streams to Reactive Streams
From Streams to Reactive StreamsFrom Streams to Reactive Streams
From Streams to Reactive Streams
Oleg Tsal-Tsalko
 
JUG UA AdoptJSR participation
JUG UA AdoptJSR participationJUG UA AdoptJSR participation
JUG UA AdoptJSR participation
Oleg Tsal-Tsalko
 
Develop modern apps using Spring ecosystem at time of BigData
Develop modern apps using Spring ecosystem at time of BigData Develop modern apps using Spring ecosystem at time of BigData
Develop modern apps using Spring ecosystem at time of BigData
Oleg Tsal-Tsalko
 
Java 8 date & time javaday2014
Java 8 date & time javaday2014Java 8 date & time javaday2014
Java 8 date & time javaday2014
Oleg Tsal-Tsalko
 
Enterprise Integration Patterns
Enterprise Integration PatternsEnterprise Integration Patterns
Enterprise Integration Patterns
Oleg Tsal-Tsalko
 
Distributed systems and scalability rules
Distributed systems and scalability rulesDistributed systems and scalability rules
Distributed systems and scalability rules
Oleg Tsal-Tsalko
 
JUG involvment in JCP and AdopJSR program
JUG involvment in JCP and AdopJSR programJUG involvment in JCP and AdopJSR program
JUG involvment in JCP and AdopJSR program
Oleg Tsal-Tsalko
 
Ad

Recently uploaded (20)

QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 

Java 8 features

  • 1. 10+ new features you ought to know using Java 8 Oleg Tsal-Tsalko JavaDay Lviv 2015
  • 2. LEAD SOFTWARE ENGINEER AT EPAM SYSTEMS. PATIONATE DEVELOPER, SPEAKER, ACTIVE MEMBER OF KIEV JUG. PARTICIPATE IN DIFFERENT EDUCATIONAL INITIATIVES, ENGINEERING EVENTS AND JCP/ADOPTJSR PROGRAMS. OLEG TSAL-TSALKO ABOUT ME
  • 3. 3CONFIDENTIAL •  Stream API •  Lambdas and method references •  Default methods •  Optional values •  Date & Time API •  Stamped Locks and Concurrent Adders •  Type annotations and repeated annotations •  New files operations •  Overflow operations and Base64 encoding •  Nashorn JavaScript engine Agenda
  • 4. 4CONFIDENTIAL •  An abstraction that supports bulk operations over sequence of elements •  Not a data structure •  Simple and logical chaining of operations •  Lazy evaluation •  Internal parallelism if required •  Concept of functional programming in Java #1 Stream API
  • 5. 5CONFIDENTIAL •  Collection.stream() •  IntStream.range() •  Stream.of() •  Arrays.stream() •  BufferedReader.lines() •  CharSequence.chars() •  Pattern.splitAsStream() How can I get a Stream?
  • 6. 6CONFIDENTIAL Fluent and simple txns.stream() .filter(t ->t.getBuyer().getAge()>=65) .map(Txn::getSeller) .distinct() .sort(comparing(Seller::getName)) .forEach(s -> System.out.println(s.getName());
  • 7. 7CONFIDENTIAL java.util.function Package: Predicate<T> Determine if the input of type T matches some criteria Consumer<T> Accept a single input argument of type T, and return no result Function<T, R> Apply a function to the input type T, generating a result of type R java.util.stream Package: Collector<T, A, R> Used to collect stream of elements of type T into single result of type R Collectors Contains number of predefined Collectors Functional interfaces
  • 8. 8CONFIDENTIAL A lambda expression is an anonymous function –  Argument list, a return type, and a body (Object o) -> o.toString() –  Method reference Object::toString –  Capture value from enclosing context (Person p) ->p.getName().equals(name) –  Type inference p -> p.getName().equals(name) –  Forget to use anonymous classes You can now pass behavior as a data between your objects #2 Lambdas
  • 9. 9CONFIDENTIAL #3 Method reference FileFilter x = (File f) -> f.canRead(); FileFilter x = File::canRead; FileFilter x = new FileFilter() { public boolean accept(File f) { return f.canRead(); } };
  • 10. 10CONFIDENTIAL Since Java doesn’t have Function type on it’s own Lambda’s type evaluated to some Functional interface: Runnable r = () -> { doSmth(); }; Callable c = () -> calcResult(); Comparator comp = (o1, o2) -> compare(o1, o2); What’s your type?
  • 11. 11CONFIDENTIAL Influence on existing classes java.lang.Iterable#forEach() java.util.Collection#stream() java.util.Collection#parallelStream() java.util.Comparator#comparing(…) java.util.Comparator#thenComparing(…) java.util.Comparator#reverse() …
  • 12. 12CONFIDENTIAL SHOW ME THE CODE Lambdas and streams TALK IS CHEAP
  • 13. 13CONFIDENTIAL •  Libraries need to evolve, or they stagnate •  Java preserves compatibility •  Multiple inheritance in Java? •  Default methods can reduce implementation burden •  Resolution rules: – Class wins over interface – More specific subclass wins – No 3rd rule #4 Default methods
  • 14. 14CONFIDENTIAL •  Annoyed by NPEs? •  Should your method return null or throw RuntimeException? •  Wondering how to implement SpecialCase pattern? •  Inspired by functional programming and want to build a method chain for NULL handling? => You are lucky with Java 8… #5 Optional
  • 15. 15CONFIDENTIAL #6 Date & Time API •  Replaces old ambiguous java.util.Date, Calendar, TimeZone, DateFormat classes with lots of deprecations •  More fluent/simple/clean API •  Immutable classes •  Using Java8 features including lambdas •  Precise separation of concepts
  • 16. 16CONFIDENTIAL •  LocalDate – a date only •  LocalTime – a time only •  LocalDateTime – date with time •  ZonedDateTime – date with time in time zone •  Period - date-based amount of time •  Duration - time-based amount of time •  And more… Range of types
  • 17. 17CONFIDENTIAL Time Zones If you can avoid using TimeZones – do it! If not – use ZonedDateAndTime!
  • 18. 18CONFIDENTIAL •  ZoneId – replacement for TimeZone class (e.g. “Europe/London”, “Europe/Kiev”) •  ZoneOffset – representing offset from UTC time •  ZoneRules – behind the scenes class which defines time zone rules •  ZonedDateTime – main date/time class which is aware of time zones TimeZone classes in Java 8
  • 19. 19CONFIDENTIAL New API is very flexible because it based on number of abstractions at it’s bottom: Temporal – parent class for all date/time objects which defines mutation operation for them such as plus/minus/ with TemporalAdjuster – functional interface which responsible for mutating Temporal objects TemporalField – represents parts/fields of date/time objects such as (DAY_OF_WEEK, MONTH, etc.) TemporalUnit – represents type of date/time values such as (MINUTES, DAYS, YEARS, etc.) TemporalAmount – class which represents amount of time Power of abstraction
  • 20. 20CONFIDENTIAL SHOW ME THE CODE Optional and Date and Time API TALK IS CHEAP
  • 21. 21CONFIDENTIAL •  Whenever we think of using RWLock •  RWLock can cause starvation of readers •  RWLock is pessimistic on reads •  Read lock is optimistic •  Optimistic lock can be converted to pessimistic lock if necessary •  Write locks are always pessimistic #7 StampedLock
  • 22. 22CONFIDENTIAL •  [Java 5] When readers are given priority, then writers might never be able to complete •  [Java 6] But when writers are given priority, readers might be starved ⇒ In Java 8 use StampedLock instead! RWLock starvation
  • 23. 23CONFIDENTIAL •  Pros – Has much better performance than ReentrantReadWriteLock – Latest versions do not suffer from starvation of writers •  Cons – Idioms are more difficult to get right than with ReadWriteLock – A small difference can make a big difference in performance StampedLock
  • 24. 24CONFIDENTIAL How would you implement counter in multithreaded environment? •  Dirty counters (silly) •  Synchronized (very slow) •  RWLock (slow) •  Volatile (only if you have 1 writer/updater) •  AtomicInteger (yes, before Java 8…) ⇒  LongAdder in Java 8! #8 Concurrent Adders
  • 26. 26CONFIDENTIAL 9 Improved annotations 10 New file operations 11 Overflow operations 12 Base64 encoding 13 Nashorn JavaScript engine
  • 27. 27CONFIDENTIAL •  Process Termination •  Strong Random Generation •  Date.toInstant() •  Interface static methods •  Better Type Inference •  Parameter names support by compiler •  Parallel Arrays •  Nashorn CLI JavaScript interpriter •  Class dependency analyzer However there are even more…
  • 28. 28CONFIDENTIAL •  https://www.google.com/?gws_rd=ssl •  https://github.com/RichardWarburton/java-8- lambdas-exercises •  http://javaspecialists.eu/talks/jfokus13/ PhaserAndStampedLock.pdf •  http://www.javacodegeeks.com/2014/05/ java-8-features-tutorial.html •  http://blog.takipi.com/10-features-in-java-8- you-havent-heard-of/ Resources