SlideShare a Scribd company logo
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

PPTX
Java 8 - Features Overview
PDF
Java 8
PPTX
Ninth session
PDF
Lambdas HOL
PDF
Java 8 lambda expressions
PPTX
java 8 new features
PDF
Python to scala
PPT
Reactive cocoa
Java 8 - Features Overview
Java 8
Ninth session
Lambdas HOL
Java 8 lambda expressions
java 8 new features
Python to scala
Reactive cocoa

What's hot (20)

PPTX
New Features of JAVA SE8
PPTX
Java 8 new features
PPT
Introduction To Functional Programming
PDF
Functional programming in java 8 by harmeet singh
PPTX
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
PPTX
Operator overloading
PPT
Operator Overloading
PDF
Data Summer Conf 2018, “Hands-on with Apache Spark for Beginners (ENG)” — Akm...
PPTX
Java 8 streams
PPTX
Java.util.concurrent.concurrent hashmap
ODP
Functors, Applicatives and Monads In Scala
PPT
Operator overloading
PPTX
Operator overloading
PPT
jimmy hacking (at) Microsoft
PPT
Operator overloading
PPTX
Operator overloading and type conversion in cpp
PDF
Python Programming - VII. Customizing Classes and Operator Overloading
PPT
Operator overloading
PDF
Reactive Programming in the Browser feat. Scala.js and PureScript
PDF
Operator overloading
New Features of JAVA SE8
Java 8 new features
Introduction To Functional Programming
Functional programming in java 8 by harmeet singh
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Operator overloading
Operator Overloading
Data Summer Conf 2018, “Hands-on with Apache Spark for Beginners (ENG)” — Akm...
Java 8 streams
Java.util.concurrent.concurrent hashmap
Functors, Applicatives and Monads In Scala
Operator overloading
Operator overloading
jimmy hacking (at) Microsoft
Operator overloading
Operator overloading and type conversion in cpp
Python Programming - VII. Customizing Classes and Operator Overloading
Operator overloading
Reactive Programming in the Browser feat. Scala.js and PureScript
Operator overloading
Ad

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

PPTX
New Features in JDK 8
PPTX
Java 8 - An Overview
PPTX
New features in jdk8 iti
PPTX
A Brief Conceptual Introduction to Functional Java 8 and its API
PPTX
Xebicon2013 scala vsjava_final
PPTX
Java 7 & 8 - A&BP CC
PDF
Smart Migration to JDK 8
PDF
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
PDF
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
PPTX
Java 8 Intro - Core Features
PPTX
Java 7 & 8
PDF
Java 8 features
PDF
Lambdas and Streams Master Class Part 2
PPTX
Java8: what's new and what's hot
PPTX
Functional programming
PPTX
Java 8 lambda
PPTX
ODP
Functional Programming With Scala
PPTX
What`s New in Java 8
New Features in JDK 8
Java 8 - An Overview
New features in jdk8 iti
A Brief Conceptual Introduction to Functional Java 8 and its API
Xebicon2013 scala vsjava_final
Java 7 & 8 - A&BP CC
Smart Migration to JDK 8
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Java 8 Intro - Core Features
Java 7 & 8
Java 8 features
Lambdas and Streams Master Class Part 2
Java8: what's new and what's hot
Functional programming
Java 8 lambda
Functional Programming With Scala
What`s New in Java 8
Ad

Recently uploaded (20)

PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPT
Project quality management in manufacturing
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Digital Logic Computer Design lecture notes
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
web development for engineering and engineering
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Model Code of Practice - Construction Work - 21102022 .pdf
Internet of Things (IOT) - A guide to understanding
Project quality management in manufacturing
Foundation to blockchain - A guide to Blockchain Tech
Digital Logic Computer Design lecture notes
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
bas. eng. economics group 4 presentation 1.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Lecture Notes Electrical Wiring System Components
OOP with Java - Java Introduction (Basics)
web development for engineering and engineering
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
R24 SURVEYING LAB MANUAL for civil enggi

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

  • #7: 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]
  • #24: Đố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.*
  • #28: 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.