SlideShare a Scribd company logo
1 of 29
Download to read offline
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

More Related Content

What's hot

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 8Simon 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 8Takipi
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsNewCircle Training
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8icarter09
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7Deniz Oguz
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8LivePerson
 
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 JavaErhan Bagdemir
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project LambdaRahman 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 RecapDave 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 singhHarmeet Singh(Taara)
 

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
 
Java SE 8 library design
Java SE 8 library designJava SE 8 library design
Java SE 8 library design
 
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
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
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
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Java SE 8
Java SE 8Java SE 8
Java SE 8
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
 
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)
 
Smart Migration to JDK 8
Smart Migration to JDK 8Smart Migration to JDK 8
Smart Migration to JDK 8
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
 
Java 8
Java 8Java 8
Java 8
 
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
 
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
 

Similar to 10+ new Java 8 features you need to know

Java Closures
Java ClosuresJava Closures
Java ClosuresBen Evans
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinayViplav Jain
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scalafanf42
 
The Rise of Functional Programming
The Rise of Functional ProgrammingThe Rise of Functional Programming
The Rise of Functional ProgrammingTjerk Wolterink
 
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 IGNManish Pandit
 
Scala Programming Introduction
Scala Programming IntroductionScala Programming Introduction
Scala Programming IntroductionairisData
 
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.2014Metosin Oy
 
Typesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and PlayTypesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and PlayLuka Zakrajšek
 
JSR 335 / java 8 - update reference
JSR 335 / java 8 - update referenceJSR 335 / java 8 - update reference
JSR 335 / java 8 - update referencesandeepji_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 3rdprat0ham
 
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 OderskySpark 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 XitrumNgoc Dao
 
JAVA 8 Parallel Stream
JAVA 8 Parallel StreamJAVA 8 Parallel Stream
JAVA 8 Parallel StreamTengwen Wang
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usageAsmaShaikh478737
 
Introduction to Scala language
Introduction to Scala languageIntroduction to Scala language
Introduction to Scala languageAaqib Pervaiz
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, androidi 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
 

Similar to 10+ new Java 8 features you need to know (20)

Java Closures
Java ClosuresJava Closures
Java Closures
 
Scala-Ls1
Scala-Ls1Scala-Ls1
Scala-Ls1
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
 
The Rise of Functional Programming
The Rise of Functional ProgrammingThe Rise of Functional Programming
The Rise of Functional Programming
 
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
 
Scala Programming Introduction
Scala Programming IntroductionScala Programming Introduction
Scala Programming Introduction
 
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
 
Typesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and PlayTypesafe stack - Scala, Akka and Play
Typesafe stack - Scala, Akka and Play
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
JSR 335 / java 8 - update reference
JSR 335 / java 8 - update referenceJSR 335 / java 8 - update reference
JSR 335 / java 8 - update reference
 
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
 
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
 
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
 
JAVA 8 Parallel Stream
JAVA 8 Parallel StreamJAVA 8 Parallel Stream
JAVA 8 Parallel Stream
 
What’s new in java 8
What’s new in java 8What’s new in java 8
What’s new in java 8
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usage
 
Introduction to Scala language
Introduction to Scala languageIntroduction to Scala language
Introduction to Scala language
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
 
Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)
 

More from 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 StreamsOleg Tsal-Tsalko
 
JUG UA AdoptJSR participation
JUG UA AdoptJSR participationJUG UA AdoptJSR participation
JUG UA AdoptJSR participationOleg 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 javaday2014Oleg Tsal-Tsalko
 
Enterprise Integration Patterns
Enterprise Integration PatternsEnterprise Integration Patterns
Enterprise Integration PatternsOleg Tsal-Tsalko
 
Distributed systems and scalability rules
Distributed systems and scalability rulesDistributed systems and scalability rules
Distributed systems and scalability rulesOleg 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 programOleg Tsal-Tsalko
 

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)
 
Developer on a mission
Developer on a missionDeveloper on a mission
Developer on a mission
 
From Streams to Reactive Streams
From Streams to Reactive StreamsFrom Streams to Reactive Streams
From Streams to Reactive Streams
 
Java 9 Jigsaw HackDay
Java 9 Jigsaw HackDayJava 9 Jigsaw HackDay
Java 9 Jigsaw HackDay
 
JUG UA AdoptJSR participation
JUG UA AdoptJSR participationJUG UA AdoptJSR participation
JUG UA AdoptJSR participation
 
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
 
Lambdas HOL
Lambdas HOLLambdas HOL
Lambdas HOL
 
Java 8 date & time javaday2014
Java 8 date & time javaday2014Java 8 date & time javaday2014
Java 8 date & time javaday2014
 
Java 8 date & time
Java 8 date & timeJava 8 date & time
Java 8 date & time
 
Get ready for spring 4
Get ready for spring 4Get ready for spring 4
Get ready for spring 4
 
Enterprise Integration Patterns
Enterprise Integration PatternsEnterprise Integration Patterns
Enterprise Integration Patterns
 
Distributed systems and scalability rules
Distributed systems and scalability rulesDistributed systems and scalability rules
Distributed systems and scalability rules
 
Next stop: Spring 4
Next stop: Spring 4Next stop: Spring 4
Next stop: Spring 4
 
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
 

Recently uploaded

CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESkarthi keyan
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfChristianCDAM
 
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMSHigh Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMSsandhya757531
 
Robotics Group 10 (Control Schemes) cse.pdf
Robotics Group 10  (Control Schemes) cse.pdfRobotics Group 10  (Control Schemes) cse.pdf
Robotics Group 10 (Control Schemes) cse.pdfsahilsajad201
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfPaper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfNainaShrivastava14
 
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.elesangwon
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewsandhya757531
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Communityprachaibot
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdfAkritiPradhan2
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHSneha Padhiar
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solidnamansinghjarodiya
 
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxTurn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxStephen Sitton
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Sumanth A
 

Recently uploaded (20)

CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdf
 
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMSHigh Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
 
Robotics Group 10 (Control Schemes) cse.pdf
Robotics Group 10  (Control Schemes) cse.pdfRobotics Group 10  (Control Schemes) cse.pdf
Robotics Group 10 (Control Schemes) cse.pdf
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfPaper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
 
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overview
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Community
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solid
 
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxTurn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptx
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
 
Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 

10+ new Java 8 features you need to know

  • 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