SlideShare a Scribd company logo
1 of 57
Java 8 Stream & C# 3.5 
Trần Duy Quang – May 2014
Agenda 
1. Some notable new things in Java 8 
2. Comparison with LINQ in C# 3.5! 
2
1 
Some new things in Java 8
Notable things 
 Lambda expressions 
 Default methods 
 Method reference 
 Stream 
 Optional 
 New Date & Time API 
 Functional programming 
 Reference (blog post): Everything about Java 8 
4
Lambda expression 
5 
(x, y) -> x + y
Passing function around 
 Dynamically (usually weakly) typed: Javascript 
 Strongly typed: Ruby, Scala, Closure 
6 
Concise Useful 
Paralel 
lizable 
Functional approach
Why lambda in Java now? 
 Clearer than anonymous inner class 
 Basic for stream library 
 shapes.forEach(s -> s.setColor(Color.RED)); 
 A step toward functional programming ! 
7
Main advantage of lambdas 
 Concise & expressive 
8
New way of thinking 
 Encourage functional programming 
 Many classes of problems are easier to solve 
 Code is clearer to read, simpler to maintain 
9
What we actually get? 
 An instance of a class that implements the 
interface that was expected in that place 
 Remember this? 
 Interface that has exactly one abstract method! 
 Functional Interface | Single Abstract Method 
10
Shortening lambda syntax 
 Omit parameter types 
 Expressions instead of blocks 
 Single arguments? Omit parenthesis! 
11
Even better with high-order function 
 High-order function: method return lambdas 
 comparing method of Comparator need function 
specifying how to extract the key 
12
Method reference 
13
Core idea behind 
 Make lambda more succinct! 
 Rather than 
 angles.map(x -> Math.sin(x)); 
 Can be shorthand 
 angles.map(Math::sin); 
14
Predicate 
15
Core idea behind 
 Boolean test(T t) 
 A function to test condition 
16
Benefit: Flexibility 
 Even better with stream (later slides) 
17
Similar things 
 Predicate: boolean test(T t) 
 Check a condition 
 Consumer: void consume(T t) 
 Perform action with the given object 
 BiConsumer: with two parameters 
 Function: R change(T t) 
 Take an object type T and return new object type R 
 BiFunction: with two parameters 
 Supplier: T supply(T t) 
 Return an object with the same type 
18
More convenient helper classes 
 IntConsumer 
 IntFunction<R> 
 IntPredicate 
 IntSupplier 
19
Stream 
20
Stream 
 Wrapper around data sources: arrays, collections… 
 Use lambdas 
 Support map/reduce 
 Lazy evaluation 
 Made parallel atutomatically by compiler 
21
Making streams 
 From individual values 
 Stream.of(val1, val2,…) 
 From array 
 Stream.of(names), Arrays.stream(names) 
 From List (and other collections) 
 names.stream() 
 From a StreamBuilder 
 builder.build() 
 From String 
 String.chars, Arrays.stream(s.split()) 
 From another stream 
 distinct, filter, map, limit, sorted, substream 
22
Turn stream to other data structures 
 Array 
 employees.toArray(Employee[]::new); 
 List 
 names.collect(Collectors.toList()); 
 Set 
 names.collect(Collectors.toSet()); 
23
2 
Comparison with LINQ in C#
Restriction Operators 
25
1. Filtering 
 Find names where “am” occurs 
26
2. Indexed Filtering (tricky) 
 Find names where length <= index + 1 
(generate an indexed stream out of the original array) 
27
Projection Operators 
28
3. Selecting/Mapping 
 Add “Hello “ in front of each names 
29
4. Selecting Many/Flattening 
 Project all the elements in a single collection 
 Java: Transform to entry set, then flatten 
30
Partitioning Operators 
31
5. Taking an Arbitrary Number of Items 
 Obtain the first 4 items 
 Java: convert IntStream into Stream<Integer> 
32
6. Taking Items Based on Predicate 
 Take while having the string that start with “S” 
 Java: don’t have the short-circuited ability, have to 
create Boolean map 
33 
Different meaning from the above!
7. Skipping an Arbitrary Number of Items 
 Skip top items, take the rest 
 Java: 
34
8. Skipping Items Based on Predicate 
 LINQ: SkipWhile 
 Sadly, no way in Java  
35
Ordering Operators 
36
9. Ordering/Sorting Elements 
 Order the elements of a collection alphabetically 
 Java: 
37
10. Ordering/Sorting Elements by 
Specific Criterium 
 Ordering by the length of the string 
 Java 
 Shorthand: 
38
11. Ordering/Sorting Elements by 
Multiple Criteria 
 Sort by length, then by order 
 Java: 
39
Grouping Operators 
40
12.Grouping by a Criterium 
 Group collection of strings by their length 
41
Set Operators 
42
13. Filter Distinct Elements 
 Obtain all the distinct elements from a collection 
43
14. Union of Two Sets 
 Join together two sets of items 
44
Element Operatos 
45
15. First Element 
 Obtain the first element of a collection 
 Java: Maybe better! 
46
Range Operators 
47
16. Generate a Range of Numbers 
 Generate a range of no that are multiples of 11 
48
Quantifier Operators 
49
17. All 
 Do all elements in a collection satisfy a predicate? 
50
18. Any 
 Do any elements in a collection satisfy a predicate? 
51
Merging Operators 
52
19.Zip 
 Combine two collections into a single collection 
53
The last coffee drop 
54
Still left behind by C#! Gambatte, Java!
Make IntelliJ IDEA work with Java 8 
 Make sure you have the latest version (>=13.1.2) 
 Change project language level to 8.0 (F4) 
56
Reference 
 Blog post: Java Streams Preview vs .Net High- 
Order Programming with LINQ 
 Slide: Evolution of Java 
 Slide: Lambda expressions & Stream in Java 8 
 Slide: Java 8 Stream Tutorial Part 1 
 Slide: Java 8 Stream Tutorial part 2 
 Just for fun (Youtube Video 20”): 
 Javapocalypse ^^ 
57

More Related Content

What's hot

New Features of JAVA SE8
New Features of JAVA SE8New Features of JAVA SE8
New Features of JAVA SE8Dinesh Pathak
 
Introduction To Functional Programming
Introduction To Functional ProgrammingIntroduction To Functional Programming
Introduction To Functional Programmingnewmedio
 
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)
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Harmeet Singh(Taara)
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingBurhan Ahmed
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Data Summer Conf 2018, “Hands-on with Apache Spark for Beginners (ENG)” — Akm...
Data Summer Conf 2018, “Hands-on with Apache Spark for Beginners (ENG)” — Akm...Data Summer Conf 2018, “Hands-on with Apache Spark for Beginners (ENG)” — Akm...
Data Summer Conf 2018, “Hands-on with Apache Spark for Beginners (ENG)” — Akm...Provectus
 
Java.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmapJava.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmapSrinivasan Raghvan
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaKnoldus Inc.
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingabhay singh
 
jimmy hacking (at) Microsoft
jimmy hacking (at) Microsoftjimmy hacking (at) Microsoft
jimmy hacking (at) MicrosoftJimmy Schementi
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpprajshreemuthiah
 
Python Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingPython Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingRanel Padon
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingArunaDevi63
 
Reactive Programming in the Browser feat. Scala.js and PureScript
Reactive Programming in the Browser feat. Scala.js and PureScriptReactive Programming in the Browser feat. Scala.js and PureScript
Reactive Programming in the Browser feat. Scala.js and PureScriptLuka Jacobowitz
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKamal Acharya
 

What's hot (20)

New Features of JAVA SE8
New Features of JAVA SE8New Features of JAVA SE8
New Features of JAVA SE8
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
Introduction To Functional Programming
Introduction To Functional ProgrammingIntroduction To Functional Programming
Introduction To Functional Programming
 
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
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Data Summer Conf 2018, “Hands-on with Apache Spark for Beginners (ENG)” — Akm...
Data Summer Conf 2018, “Hands-on with Apache Spark for Beginners (ENG)” — Akm...Data Summer Conf 2018, “Hands-on with Apache Spark for Beginners (ENG)” — Akm...
Data Summer Conf 2018, “Hands-on with Apache Spark for Beginners (ENG)” — Akm...
 
Java 8 streams
Java 8 streams Java 8 streams
Java 8 streams
 
Java.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmapJava.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmap
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In Scala
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
jimmy hacking (at) Microsoft
jimmy hacking (at) Microsoftjimmy hacking (at) Microsoft
jimmy hacking (at) Microsoft
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
 
Python Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingPython Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator Overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Reactive Programming in the Browser feat. Scala.js and PureScript
Reactive Programming in the Browser feat. Scala.js and PureScriptReactive Programming in the Browser feat. Scala.js and PureScript
Reactive Programming in the Browser feat. Scala.js and PureScript
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 

Similar to Java 8 stream and c# 3.5

New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An OverviewIndrajit Das
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 itiAhmed mar3y
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIJörn Guy Süß JGS
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalUrs Peter
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsEmiel Paasschens
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2José Paumard
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hotSergii Maliarov
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project LambdaRahman USTA
 

Similar to Java 8 stream and c# 3.5 (20)

New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 iti
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
 
Java 7 & 8 - A&BP CC
Java 7 & 8 - A&BP CCJava 7 & 8 - A&BP CC
Java 7 & 8 - A&BP CC
 
Smart Migration to JDK 8
Smart Migration to JDK 8Smart Migration to JDK 8
Smart Migration to JDK 8
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
 
Java 7 & 8
Java 7 & 8Java 7 & 8
Java 7 & 8
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Java 8
Java 8Java 8
Java 8
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
 
Lambdas and Laughs
Lambdas and LaughsLambdas and Laughs
Lambdas and Laughs
 

Recently uploaded

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 

Recently uploaded (20)

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 

Java 8 stream and c# 3.5

  • 1. Java 8 Stream & C# 3.5 Trần Duy Quang – May 2014
  • 2. Agenda 1. Some notable new things in Java 8 2. Comparison with LINQ in C# 3.5! 2
  • 3. 1 Some new things in Java 8
  • 4. Notable things  Lambda expressions  Default methods  Method reference  Stream  Optional  New Date & Time API  Functional programming  Reference (blog post): Everything about Java 8 4
  • 5. Lambda expression 5 (x, y) -> x + y
  • 6. Passing function around  Dynamically (usually weakly) typed: Javascript  Strongly typed: Ruby, Scala, Closure 6 Concise Useful Paralel lizable Functional approach
  • 7. Why lambda in Java now?  Clearer than anonymous inner class  Basic for stream library  shapes.forEach(s -> s.setColor(Color.RED));  A step toward functional programming ! 7
  • 8. Main advantage of lambdas  Concise & expressive 8
  • 9. New way of thinking  Encourage functional programming  Many classes of problems are easier to solve  Code is clearer to read, simpler to maintain 9
  • 10. What we actually get?  An instance of a class that implements the interface that was expected in that place  Remember this?  Interface that has exactly one abstract method!  Functional Interface | Single Abstract Method 10
  • 11. Shortening lambda syntax  Omit parameter types  Expressions instead of blocks  Single arguments? Omit parenthesis! 11
  • 12. Even better with high-order function  High-order function: method return lambdas  comparing method of Comparator need function specifying how to extract the key 12
  • 14. Core idea behind  Make lambda more succinct!  Rather than  angles.map(x -> Math.sin(x));  Can be shorthand  angles.map(Math::sin); 14
  • 16. Core idea behind  Boolean test(T t)  A function to test condition 16
  • 17. Benefit: Flexibility  Even better with stream (later slides) 17
  • 18. Similar things  Predicate: boolean test(T t)  Check a condition  Consumer: void consume(T t)  Perform action with the given object  BiConsumer: with two parameters  Function: R change(T t)  Take an object type T and return new object type R  BiFunction: with two parameters  Supplier: T supply(T t)  Return an object with the same type 18
  • 19. More convenient helper classes  IntConsumer  IntFunction<R>  IntPredicate  IntSupplier 19
  • 21. Stream  Wrapper around data sources: arrays, collections…  Use lambdas  Support map/reduce  Lazy evaluation  Made parallel atutomatically by compiler 21
  • 22. Making streams  From individual values  Stream.of(val1, val2,…)  From array  Stream.of(names), Arrays.stream(names)  From List (and other collections)  names.stream()  From a StreamBuilder  builder.build()  From String  String.chars, Arrays.stream(s.split())  From another stream  distinct, filter, map, limit, sorted, substream 22
  • 23. Turn stream to other data structures  Array  employees.toArray(Employee[]::new);  List  names.collect(Collectors.toList());  Set  names.collect(Collectors.toSet()); 23
  • 24. 2 Comparison with LINQ in C#
  • 26. 1. Filtering  Find names where “am” occurs 26
  • 27. 2. Indexed Filtering (tricky)  Find names where length <= index + 1 (generate an indexed stream out of the original array) 27
  • 29. 3. Selecting/Mapping  Add “Hello “ in front of each names 29
  • 30. 4. Selecting Many/Flattening  Project all the elements in a single collection  Java: Transform to entry set, then flatten 30
  • 32. 5. Taking an Arbitrary Number of Items  Obtain the first 4 items  Java: convert IntStream into Stream<Integer> 32
  • 33. 6. Taking Items Based on Predicate  Take while having the string that start with “S”  Java: don’t have the short-circuited ability, have to create Boolean map 33 Different meaning from the above!
  • 34. 7. Skipping an Arbitrary Number of Items  Skip top items, take the rest  Java: 34
  • 35. 8. Skipping Items Based on Predicate  LINQ: SkipWhile  Sadly, no way in Java  35
  • 37. 9. Ordering/Sorting Elements  Order the elements of a collection alphabetically  Java: 37
  • 38. 10. Ordering/Sorting Elements by Specific Criterium  Ordering by the length of the string  Java  Shorthand: 38
  • 39. 11. Ordering/Sorting Elements by Multiple Criteria  Sort by length, then by order  Java: 39
  • 41. 12.Grouping by a Criterium  Group collection of strings by their length 41
  • 43. 13. Filter Distinct Elements  Obtain all the distinct elements from a collection 43
  • 44. 14. Union of Two Sets  Join together two sets of items 44
  • 46. 15. First Element  Obtain the first element of a collection  Java: Maybe better! 46
  • 48. 16. Generate a Range of Numbers  Generate a range of no that are multiples of 11 48
  • 50. 17. All  Do all elements in a collection satisfy a predicate? 50
  • 51. 18. Any  Do any elements in a collection satisfy a predicate? 51
  • 53. 19.Zip  Combine two collections into a single collection 53
  • 54. The last coffee drop 54
  • 55. Still left behind by C#! Gambatte, Java!
  • 56. Make IntelliJ IDEA work with Java 8  Make sure you have the latest version (>=13.1.2)  Change project language level to 8.0 (F4) 56
  • 57. Reference  Blog post: Java Streams Preview vs .Net High- Order Programming with LINQ  Slide: Evolution of Java  Slide: Lambda expressions & Stream in Java 8  Slide: Java 8 Stream Tutorial Part 1  Slide: Java 8 Stream Tutorial part 2  Just for fun (Youtube Video 20”):  Javapocalypse ^^ 57

Editor's Notes

  1. In 1974, Liskov and Zilles described a strong-typed language as one in which "whenever an object is passed from a calling function to a called function, its type must be compatible with the type declared in the called function."[1] Jackson wrote, "In a strongly typed language each data area will have a distinct type and each process will state its communication requirements in terms of these types."[2]
  2. Đối với mảng thông thường là EntryType[]::new, nhưng thông thường là nhận một Supplier với đối số là một số nguyên (size) và trả về một mảng rổng. Cũng có hàm toArray không đối số, tuy nhiên nó trả về Object[], và ta không thể cast Object[] sang Blah[] cho dù các thành phần của mảng có kiểu là Blah Thường là phải import static java.util.stream.Collectors.*
  3. Now, the lack of indices in the stream made the algorithm more verbose, but it was also interesting to notice the incompatibilities between primitive-type streams, like IntStream and reference type streams like Stream<Integer>. In this case, I was forced to transform the IntStream returned byintRange into a Stream<Integer> in order to make the argument compatible with the types expected by zip as you can see in the line #4.