SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Java8
Yiguang Hu
What has been happening in
Computer
❖ Moor’s Law no longer applicable
❖ Computer speed is not increasing unlimited
❖ MultiCore Computer
❖ Pre-Java8 does not take advantage of multicore
computer Power
❖ Threading is hard, multicore make it harder
Computer Languages
❖ Groovy, Scala, Clojure, Kotlin, JavaScript/CoffeeScript,
Go, Swift, Lua, ErLang, Haskell, Ruby,C#,F#,…
❖ Closure
❖ Lambda Expression
❖ Pre-Java8 Did not have this, so what?
What is Closure
“In computer science, a closure is a first-class function with
free variables that are bound in the lexical environment.”
Groovy Closure
def myConst = 5
def incByConst = { num -> num + myConst }
println incByConst(10) //==>15
myConst = 20
println incByConst(10) //==>30
def result=[]
(1..10).collect(result,{a->a*a})
result==[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
What is Lambda Expression
❖ A concise representation of an anonymous
function that can be passed around:
❖ doesn't have a name, but has
❖ a list of parameters
❖ a body
❖ a return type
❖ possibly a list of exceptions
Example Lambda Expression
//Anonymous version
Runnable r1 = new Runnable(){
@Override
public void run(){
System.out.println("Hello world one!");
}
};
// Lambda Runnable
Runnable r2 = () -> System.out.println("Hello world two!");
// Run em!
r1.run();
r2.run();
Compare the one-liner lambda with the 4+ lines legacy code!
What’s new in Java8
❖ Lambda expression
❖ Default methods
❖ Stream
❖ optionals
❖ Date/Time API
What Lambda Expression
brings?
❖ Pass code concisely-functions are passed as values
❖ Collection:
❖ List<Person> pl ;//Given a list of Persons
❖ looping: pl.forEach( p -> p.printName() );
❖ filtering and chaining:
❖ pl.stream().filter(p->p.getAge()>30).sort(comparing((a)-
>a.getAge())).forEach(Person::printName);
❖ This one line replaces 20 lines of traditional code and does more
(later on stream)
functional interfaces
❖ functional interface specifies exactly one abstract
method
❖ Examples:
❖ Predicate<T>:boolean test(T t)
❖ Comparator<T>:compare(T o1, T o2)
Default Methods
❖ pl.stream().filter(p-
>p.getAge()>30).sort(comparing((a)-
>a.getAge()).reversed().thenComparing(Person::
getHeight())).forEach(Person::printName);
Default Methods
❖ Interface can contain method signatures for which an
implementation class doesn’t have to provide
implementation
❖ interface can now provide default method
implementation
Streams
❖ Manipulate collection of data in a declarative way.
❖ Streams can be processed in parallel transparently
without having to write any multithreaded code!
Stream example
❖ pl.stream().filter(p->p.getAge()>30).sort(comparing((a)-
>a.getAge()).reversed().thenComparing(Person::getHeigh
t())).map(Person::getName()).collect(toList))
❖ To Exploit multicore architecture and execute the code in
parallel
❖ pl.parallelStream().filter(p-
>p.getAge()>30).sort(comparing((a)-
>a.getAge()).reversed().thenComparing(Person::getHeigh
t())).map(Person::getName()).collect(toList))
Hi Map Collect/Reduce
❖ pl.parallelStream().filter(p-
>p.getAge()>30).sort(comparing((a)-
>a.getAge()).reversed().thenComparing(Person::getHei
ght())).map(Person::getName()).collect(toList))
❖ pl.parallelStream().filter(p-
>p.getAge()>30).sort(comparing((a)-
>a.getAge()).reversed().thenComparing(Person::getHei
ght())).map(Person::getSallary()).reduce(Integer::sum)).get
()
Optionals
❖ String version =
computer.getSoundcard().getUSB().getVersion();
❖ Problem: Multiple NPE possible. Traditional way:
String version = "UNKNOWN";
if(computer != null){
Soundcard soundcard = computer.getSoundcard();
if(soundcard != null){
USB usb = soundcard.getUSB();
if(usb != null){
version = usb.getVersion();
}
}
}
Optionals
❖ The problem with Null
❖ source of error: NPE
❖ bloats code
❖ meaningless
❖ break java philosophy
❖ a hole in type system
–Tony Hoare
Introducing Null reference in ALGOL W in 1965 was
“my billion-dollar mistake”
Optionals
❖ Groovy safe navigation operator+Elvis operator
❖ String version = computer?.soundcard?.uSB.version ?:
"UNKNOWN";
Optionals
❖ how to model “absence of a value”
❖ Scala introduced Option[T]
❖ Java 8 call it Optional[T]
❖ Optional<String> version =
optcomputer.map(Computer::getSoundcard).map(Sound
Card::getUSB).map(USB::getVersion)
Beyond Java 8
❖ Functional programming: Focus on What
❖ Scala example
❖ flatten(List(List(1, 1), 2, List(3, List(5, 8))))
❖ result: List(1, 1, 2, 3, 5, 8)
❖ Imperative programming: Focus on How
❖ Many lines needed to do the above 1-line job
❖ Functional programming is itself a great topic!

Weitere ähnliche Inhalte

Was ist angesagt?

Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Ovidiu Farauanu
 
Functions - complex first class citizen
Functions - complex first class citizenFunctions - complex first class citizen
Functions - complex first class citizenVytautas Butkus
 
What make Swift Awesome
What make Swift AwesomeWhat make Swift Awesome
What make Swift AwesomeSokna Ly
 
Next Generation Language Go
Next Generation Language GoNext Generation Language Go
Next Generation Language GoYoichiro Shimizu
 
Meetup C++ A brief overview of c++17
Meetup C++  A brief overview of c++17Meetup C++  A brief overview of c++17
Meetup C++ A brief overview of c++17Daniel Eriksson
 
Dynamic Swift
Dynamic SwiftDynamic Swift
Dynamic SwiftSaul Mora
 
Ruby basics ||
Ruby basics ||Ruby basics ||
Ruby basics ||datt30
 
Linguistic Symbiosis between Actors and Threads
Linguistic Symbiosis between Actors and ThreadsLinguistic Symbiosis between Actors and Threads
Linguistic Symbiosis between Actors and ThreadsESUG
 
Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11Åsa Pehrsson
 
Object Oriented Programming - Inheritance
Object Oriented Programming - InheritanceObject Oriented Programming - Inheritance
Object Oriented Programming - InheritanceDudy Ali
 
Coding convention
Coding conventionCoding convention
Coding conventionKhoa Nguyen
 
Optimizing Communicating Event-Loop Languages with Truffle
Optimizing Communicating Event-Loop Languages with TruffleOptimizing Communicating Event-Loop Languages with Truffle
Optimizing Communicating Event-Loop Languages with TruffleStefan Marr
 
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...LogeekNightUkraine
 

Was ist angesagt? (20)

Clojure+ClojureScript Webapps
Clojure+ClojureScript WebappsClojure+ClojureScript Webapps
Clojure+ClojureScript Webapps
 
Briefly Rust
Briefly RustBriefly Rust
Briefly Rust
 
Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)
 
Functions - complex first class citizen
Functions - complex first class citizenFunctions - complex first class citizen
Functions - complex first class citizen
 
What make Swift Awesome
What make Swift AwesomeWhat make Swift Awesome
What make Swift Awesome
 
Clojure for Rubyists
Clojure for RubyistsClojure for Rubyists
Clojure for Rubyists
 
Next Generation Language Go
Next Generation Language GoNext Generation Language Go
Next Generation Language Go
 
Meetup C++ A brief overview of c++17
Meetup C++  A brief overview of c++17Meetup C++  A brief overview of c++17
Meetup C++ A brief overview of c++17
 
Dynamic Swift
Dynamic SwiftDynamic Swift
Dynamic Swift
 
Ruby basics ||
Ruby basics ||Ruby basics ||
Ruby basics ||
 
Linguistic Symbiosis between Actors and Threads
Linguistic Symbiosis between Actors and ThreadsLinguistic Symbiosis between Actors and Threads
Linguistic Symbiosis between Actors and Threads
 
Python intro
Python introPython intro
Python intro
 
Just Kotlin
Just KotlinJust Kotlin
Just Kotlin
 
Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11
 
Object Oriented Programming - Inheritance
Object Oriented Programming - InheritanceObject Oriented Programming - Inheritance
Object Oriented Programming - Inheritance
 
Oop lecture5
Oop lecture5Oop lecture5
Oop lecture5
 
Templates
TemplatesTemplates
Templates
 
Coding convention
Coding conventionCoding convention
Coding convention
 
Optimizing Communicating Event-Loop Languages with Truffle
Optimizing Communicating Event-Loop Languages with TruffleOptimizing Communicating Event-Loop Languages with Truffle
Optimizing Communicating Event-Loop Languages with Truffle
 
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
 

Ähnlich wie Java8 and Functional Programming

Concurrency in go
Concurrency in goConcurrency in go
Concurrency in goborderj
 
Go lang introduction
Go lang introductionGo lang introduction
Go lang introductionyangwm
 
10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about goDvir Volk
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 
Introduction to Go for Java Developers
Introduction to Go for Java DevelopersIntroduction to Go for Java Developers
Introduction to Go for Java DevelopersLaszlo Csontos
 
Functional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersFunctional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersJayaram Sankaranarayanan
 
Measuring maintainability; software metrics explained
Measuring maintainability; software metrics explainedMeasuring maintainability; software metrics explained
Measuring maintainability; software metrics explainedDennis de Greef
 
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Raffi Khatchadourian
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golangBasil N G
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Raffi Khatchadourian
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Ganesh Samarthyam
 
Beyond PITS, Functional Principles for Software Architecture
Beyond PITS, Functional Principles for Software ArchitectureBeyond PITS, Functional Principles for Software Architecture
Beyond PITS, Functional Principles for Software ArchitectureJayaram Sankaranarayanan
 
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisCompiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisEelco Visser
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Ovidiu Farauanu
 

Ähnlich wie Java8 and Functional Programming (20)

Concurrency in go
Concurrency in goConcurrency in go
Concurrency in go
 
Go lang introduction
Go lang introductionGo lang introduction
Go lang introduction
 
Loom and concurrency latest
Loom and concurrency latestLoom and concurrency latest
Loom and concurrency latest
 
10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about go
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Introduction to Go for Java Developers
Introduction to Go for Java DevelopersIntroduction to Go for Java Developers
Introduction to Go for Java Developers
 
Functional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersFunctional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 Developers
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Measuring maintainability; software metrics explained
Measuring maintainability; software metrics explainedMeasuring maintainability; software metrics explained
Measuring maintainability; software metrics explained
 
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Java 8
Java 8Java 8
Java 8
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 
Async fun
Async funAsync fun
Async fun
 
Beyond PITS, Functional Principles for Software Architecture
Beyond PITS, Functional Principles for Software ArchitectureBeyond PITS, Functional Principles for Software Architecture
Beyond PITS, Functional Principles for Software Architecture
 
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisCompiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow Analysis
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 

Mehr von Yiguang Hu

Data analysis scala_spark
Data analysis scala_sparkData analysis scala_spark
Data analysis scala_sparkYiguang Hu
 
Introduction to Vert.x
Introduction to Vert.xIntroduction to Vert.x
Introduction to Vert.xYiguang Hu
 
Cross platform Mobile development on Titanium
Cross platform Mobile development on TitaniumCross platform Mobile development on Titanium
Cross platform Mobile development on TitaniumYiguang Hu
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web ToolkitsYiguang Hu
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web ToolkitsYiguang Hu
 

Mehr von Yiguang Hu (10)

Data analysis scala_spark
Data analysis scala_sparkData analysis scala_spark
Data analysis scala_spark
 
Introduction to Vert.x
Introduction to Vert.xIntroduction to Vert.x
Introduction to Vert.x
 
Cross platform Mobile development on Titanium
Cross platform Mobile development on TitaniumCross platform Mobile development on Titanium
Cross platform Mobile development on Titanium
 
Phone Gap
Phone GapPhone Gap
Phone Gap
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web Toolkits
 
Clojure
ClojureClojure
Clojure
 
Why Grails
Why GrailsWhy Grails
Why Grails
 
Why Grails?
Why Grails?Why Grails?
Why Grails?
 
Gsword
GswordGsword
Gsword
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web Toolkits
 

Kürzlich hochgeladen

Application deck- Cyril Caudroy-2024.pdf
Application deck- Cyril Caudroy-2024.pdfApplication deck- Cyril Caudroy-2024.pdf
Application deck- Cyril Caudroy-2024.pdfCyril CAUDROY
 
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书saphesg8
 
办理(Hull毕业证书)英国赫尔大学毕业证成绩单原版一比一
办理(Hull毕业证书)英国赫尔大学毕业证成绩单原版一比一办理(Hull毕业证书)英国赫尔大学毕业证成绩单原版一比一
办理(Hull毕业证书)英国赫尔大学毕业证成绩单原版一比一F La
 
定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一
定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一
定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一z xss
 
定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一z zzz
 
Digital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Discovery Institute
 
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一diploma 1
 
Graduate Trainee Officer Job in Bank Al Habib 2024.docx
Graduate Trainee Officer Job in Bank Al Habib 2024.docxGraduate Trainee Officer Job in Bank Al Habib 2024.docx
Graduate Trainee Officer Job in Bank Al Habib 2024.docxJobs Finder Hub
 
Ethics of Animal Research Laika mission.ppt
Ethics of Animal Research Laika mission.pptEthics of Animal Research Laika mission.ppt
Ethics of Animal Research Laika mission.pptShafqatShakeel1
 
Back on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveBack on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveMarharyta Nedzelska
 
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCRdollysharma2066
 
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一2s3dgmej
 
Kindergarten-DLL-MELC-Q3-Week 2 asf.docx
Kindergarten-DLL-MELC-Q3-Week 2 asf.docxKindergarten-DLL-MELC-Q3-Week 2 asf.docx
Kindergarten-DLL-MELC-Q3-Week 2 asf.docxLesterJayAquino
 
Escort Service Andheri WhatsApp:+91-9833363713
Escort Service Andheri WhatsApp:+91-9833363713Escort Service Andheri WhatsApp:+91-9833363713
Escort Service Andheri WhatsApp:+91-9833363713Riya Pathan
 
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改yuu sss
 
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一A SSS
 
ME 205- Chapter 6 - Pure Bending of Beams.pdf
ME 205- Chapter 6 - Pure Bending of Beams.pdfME 205- Chapter 6 - Pure Bending of Beams.pdf
ME 205- Chapter 6 - Pure Bending of Beams.pdfaae4149584
 
LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024Bruce Bennett
 
Ch. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdfCh. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdfJamalYaseenJameelOde
 

Kürzlich hochgeladen (20)

Application deck- Cyril Caudroy-2024.pdf
Application deck- Cyril Caudroy-2024.pdfApplication deck- Cyril Caudroy-2024.pdf
Application deck- Cyril Caudroy-2024.pdf
 
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
 
办理(Hull毕业证书)英国赫尔大学毕业证成绩单原版一比一
办理(Hull毕业证书)英国赫尔大学毕业证成绩单原版一比一办理(Hull毕业证书)英国赫尔大学毕业证成绩单原版一比一
办理(Hull毕业证书)英国赫尔大学毕业证成绩单原版一比一
 
定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一
定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一
定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一
 
定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一
 
Digital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, India
 
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一
办理(Salford毕业证书)索尔福德大学毕业证成绩单原版一比一
 
Graduate Trainee Officer Job in Bank Al Habib 2024.docx
Graduate Trainee Officer Job in Bank Al Habib 2024.docxGraduate Trainee Officer Job in Bank Al Habib 2024.docx
Graduate Trainee Officer Job in Bank Al Habib 2024.docx
 
Ethics of Animal Research Laika mission.ppt
Ethics of Animal Research Laika mission.pptEthics of Animal Research Laika mission.ppt
Ethics of Animal Research Laika mission.ppt
 
Back on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveBack on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental Leave
 
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
 
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
 
Kindergarten-DLL-MELC-Q3-Week 2 asf.docx
Kindergarten-DLL-MELC-Q3-Week 2 asf.docxKindergarten-DLL-MELC-Q3-Week 2 asf.docx
Kindergarten-DLL-MELC-Q3-Week 2 asf.docx
 
Young Call~Girl in Pragati Maidan New Delhi 8448380779 Full Enjoy Escort Service
Young Call~Girl in Pragati Maidan New Delhi 8448380779 Full Enjoy Escort ServiceYoung Call~Girl in Pragati Maidan New Delhi 8448380779 Full Enjoy Escort Service
Young Call~Girl in Pragati Maidan New Delhi 8448380779 Full Enjoy Escort Service
 
Escort Service Andheri WhatsApp:+91-9833363713
Escort Service Andheri WhatsApp:+91-9833363713Escort Service Andheri WhatsApp:+91-9833363713
Escort Service Andheri WhatsApp:+91-9833363713
 
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改
办澳洲詹姆斯库克大学毕业证成绩单pdf电子版制作修改
 
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
 
ME 205- Chapter 6 - Pure Bending of Beams.pdf
ME 205- Chapter 6 - Pure Bending of Beams.pdfME 205- Chapter 6 - Pure Bending of Beams.pdf
ME 205- Chapter 6 - Pure Bending of Beams.pdf
 
LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024
 
Ch. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdfCh. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdf
 

Java8 and Functional Programming

  • 2. What has been happening in Computer ❖ Moor’s Law no longer applicable ❖ Computer speed is not increasing unlimited ❖ MultiCore Computer ❖ Pre-Java8 does not take advantage of multicore computer Power ❖ Threading is hard, multicore make it harder
  • 3. Computer Languages ❖ Groovy, Scala, Clojure, Kotlin, JavaScript/CoffeeScript, Go, Swift, Lua, ErLang, Haskell, Ruby,C#,F#,… ❖ Closure ❖ Lambda Expression ❖ Pre-Java8 Did not have this, so what?
  • 4. What is Closure “In computer science, a closure is a first-class function with free variables that are bound in the lexical environment.”
  • 5. Groovy Closure def myConst = 5 def incByConst = { num -> num + myConst } println incByConst(10) //==>15 myConst = 20 println incByConst(10) //==>30 def result=[] (1..10).collect(result,{a->a*a}) result==[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  • 6. What is Lambda Expression ❖ A concise representation of an anonymous function that can be passed around: ❖ doesn't have a name, but has ❖ a list of parameters ❖ a body ❖ a return type ❖ possibly a list of exceptions
  • 7. Example Lambda Expression //Anonymous version Runnable r1 = new Runnable(){ @Override public void run(){ System.out.println("Hello world one!"); } }; // Lambda Runnable Runnable r2 = () -> System.out.println("Hello world two!"); // Run em! r1.run(); r2.run(); Compare the one-liner lambda with the 4+ lines legacy code!
  • 8. What’s new in Java8 ❖ Lambda expression ❖ Default methods ❖ Stream ❖ optionals ❖ Date/Time API
  • 9. What Lambda Expression brings? ❖ Pass code concisely-functions are passed as values ❖ Collection: ❖ List<Person> pl ;//Given a list of Persons ❖ looping: pl.forEach( p -> p.printName() ); ❖ filtering and chaining: ❖ pl.stream().filter(p->p.getAge()>30).sort(comparing((a)- >a.getAge())).forEach(Person::printName); ❖ This one line replaces 20 lines of traditional code and does more (later on stream)
  • 10. functional interfaces ❖ functional interface specifies exactly one abstract method ❖ Examples: ❖ Predicate<T>:boolean test(T t) ❖ Comparator<T>:compare(T o1, T o2)
  • 12. Default Methods ❖ Interface can contain method signatures for which an implementation class doesn’t have to provide implementation ❖ interface can now provide default method implementation
  • 13. Streams ❖ Manipulate collection of data in a declarative way. ❖ Streams can be processed in parallel transparently without having to write any multithreaded code!
  • 14. Stream example ❖ pl.stream().filter(p->p.getAge()>30).sort(comparing((a)- >a.getAge()).reversed().thenComparing(Person::getHeigh t())).map(Person::getName()).collect(toList)) ❖ To Exploit multicore architecture and execute the code in parallel ❖ pl.parallelStream().filter(p- >p.getAge()>30).sort(comparing((a)- >a.getAge()).reversed().thenComparing(Person::getHeigh t())).map(Person::getName()).collect(toList))
  • 15. Hi Map Collect/Reduce ❖ pl.parallelStream().filter(p- >p.getAge()>30).sort(comparing((a)- >a.getAge()).reversed().thenComparing(Person::getHei ght())).map(Person::getName()).collect(toList)) ❖ pl.parallelStream().filter(p- >p.getAge()>30).sort(comparing((a)- >a.getAge()).reversed().thenComparing(Person::getHei ght())).map(Person::getSallary()).reduce(Integer::sum)).get ()
  • 16. Optionals ❖ String version = computer.getSoundcard().getUSB().getVersion(); ❖ Problem: Multiple NPE possible. Traditional way: String version = "UNKNOWN"; if(computer != null){ Soundcard soundcard = computer.getSoundcard(); if(soundcard != null){ USB usb = soundcard.getUSB(); if(usb != null){ version = usb.getVersion(); } } }
  • 17. Optionals ❖ The problem with Null ❖ source of error: NPE ❖ bloats code ❖ meaningless ❖ break java philosophy ❖ a hole in type system
  • 18. –Tony Hoare Introducing Null reference in ALGOL W in 1965 was “my billion-dollar mistake”
  • 19. Optionals ❖ Groovy safe navigation operator+Elvis operator ❖ String version = computer?.soundcard?.uSB.version ?: "UNKNOWN";
  • 20. Optionals ❖ how to model “absence of a value” ❖ Scala introduced Option[T] ❖ Java 8 call it Optional[T] ❖ Optional<String> version = optcomputer.map(Computer::getSoundcard).map(Sound Card::getUSB).map(USB::getVersion)
  • 21. Beyond Java 8 ❖ Functional programming: Focus on What ❖ Scala example ❖ flatten(List(List(1, 1), 2, List(3, List(5, 8)))) ❖ result: List(1, 1, 2, 3, 5, 8) ❖ Imperative programming: Focus on How ❖ Many lines needed to do the above 1-line job ❖ Functional programming is itself a great topic!