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
 
Loom and concurrency latest
Loom and concurrency latestLoom and concurrency latest
Loom and concurrency latestSrinivasan Raghavan
 
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
 
C++ theory
C++ theoryC++ theory
C++ theoryShyam Khant
 
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
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 WorkshopMario Fusco
 

Ä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
 
Phone Gap
Phone GapPhone Gap
Phone GapYiguang Hu
 
Google Web Toolkits
Google Web ToolkitsGoogle Web Toolkits
Google Web ToolkitsYiguang Hu
 
Why Grails
Why GrailsWhy Grails
Why GrailsYiguang Hu
 
Why Grails?
Why Grails?Why Grails?
Why Grails?Yiguang 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

Get To Know About "Lauren Prophet-Bryant''
Get To Know About "Lauren Prophet-Bryant''Get To Know About "Lauren Prophet-Bryant''
Get To Know About "Lauren Prophet-Bryant''Lauren Prophet-Bryant
 
Résumé (2 pager - 12 ft standard syntax)
Résumé (2 pager -  12 ft standard syntax)Résumé (2 pager -  12 ft standard syntax)
Résumé (2 pager - 12 ft standard syntax)Soham Mondal
 
Hot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night Stand
Hot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night StandHot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night Stand
Hot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night Standkumarajju5765
 
Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)
Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)
Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)sonalinghatmal
 
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...rightmanforbloodline
 
CALL ON ➄8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➄8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceCALL ON ➄8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➄8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceanilsa9823
 
CALL ON ➄8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳
CALL ON ➄8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳CALL ON ➄8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳
CALL ON ➄8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳anilsa9823
 
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 
Dark Dubai Call Girls O525547819 Skin Call Girls Dubai
Dark Dubai Call Girls O525547819 Skin Call Girls DubaiDark Dubai Call Girls O525547819 Skin Call Girls Dubai
Dark Dubai Call Girls O525547819 Skin Call Girls Dubaikojalkojal131
 
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...ranjana rawat
 
Resumes, Cover Letters, and Applying Online
Resumes, Cover Letters, and Applying OnlineResumes, Cover Letters, and Applying Online
Resumes, Cover Letters, and Applying OnlineBruce Bennett
 
Internship Report].pdf iiwmoosmsosmshkssmk
Internship Report].pdf iiwmoosmsosmshkssmkInternship Report].pdf iiwmoosmsosmshkssmk
Internship Report].pdf iiwmoosmsosmshkssmkSujalTamhane
 
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)Delhi Call girls
 
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.GabrielaMiletti
 
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...robinsonayot
 
Zeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effectZeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effectPriyanshuRawat56
 
Production Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjProduction Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjLewisJB
 

KĂŒrzlich hochgeladen (20)

Get To Know About "Lauren Prophet-Bryant''
Get To Know About "Lauren Prophet-Bryant''Get To Know About "Lauren Prophet-Bryant''
Get To Know About "Lauren Prophet-Bryant''
 
Résumé (2 pager - 12 ft standard syntax)
Résumé (2 pager -  12 ft standard syntax)Résumé (2 pager -  12 ft standard syntax)
Résumé (2 pager - 12 ft standard syntax)
 
Hot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night Stand
Hot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night StandHot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night Stand
Hot Call Girls |Delhi |Janakpuri ☎ 9711199171 Book Your One night Stand
 
Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)
Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)
Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)
 
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
TEST BANK For An Introduction to Brain and Behavior, 7th Edition by Bryan Kol...
 
CALL ON ➄8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➄8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceCALL ON ➄8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➄8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
 
VVVIP Call Girls In East Of Kailash âžĄïž Delhi âžĄïž 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In East Of Kailash âžĄïž Delhi âžĄïž 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In East Of Kailash âžĄïž Delhi âžĄïž 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In East Of Kailash âžĄïž Delhi âžĄïž 9999965857 🚀 No Advance 24HRS...
 
CALL ON ➄8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳
CALL ON ➄8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳CALL ON ➄8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳
CALL ON ➄8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳
 
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Btm Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Dark Dubai Call Girls O525547819 Skin Call Girls Dubai
Dark Dubai Call Girls O525547819 Skin Call Girls DubaiDark Dubai Call Girls O525547819 Skin Call Girls Dubai
Dark Dubai Call Girls O525547819 Skin Call Girls Dubai
 
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Saswad Call Girls Pune 8250192130Low Budget Full Independent High P...
 
Resumes, Cover Letters, and Applying Online
Resumes, Cover Letters, and Applying OnlineResumes, Cover Letters, and Applying Online
Resumes, Cover Letters, and Applying Online
 
Internship Report].pdf iiwmoosmsosmshkssmk
Internship Report].pdf iiwmoosmsosmshkssmkInternship Report].pdf iiwmoosmsosmshkssmk
Internship Report].pdf iiwmoosmsosmshkssmk
 
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Bidadi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
 
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.
 
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
 
Zeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effectZeeman Effect normal and Anomalous zeeman effect
Zeeman Effect normal and Anomalous zeeman effect
 
Production Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjProduction Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbj
 

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!