java 8 stream and c# 3.5
TRANSCRIPT
![Page 1: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/1.jpg)
Java 8 Stream & C# 3.5
Trần Duy Quang – May 2014
![Page 2: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/2.jpg)
Agenda
1. Some notable new things in Java 8
2. Comparison with LINQ in C# 3.5!
2
![Page 3: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/3.jpg)
1
Some new things in Java 8
![Page 4: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/4.jpg)
Notable things
Lambda expressions
Default methods
Method reference
Stream
Optional
New Date & Time API
Functional programming
Reference (blog post): Everything about Java 84
![Page 5: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/5.jpg)
Lambda expression
5
(x, y) -> x + y
![Page 6: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/6.jpg)
Passing function around
Dynamically (usually weakly) typed: Javascript
Strongly typed: Ruby, Scala, Closure
6
Functional approach
Concise UsefulParalellizable
![Page 7: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/7.jpg)
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
![Page 8: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/8.jpg)
Main advantage of lambdas
Concise & expressive
8
![Page 9: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/9.jpg)
New way of thinking
Encourage functional programming
Many classes of problems are easier to solve
Code is clearer to read, simpler to maintain
9
![Page 10: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/10.jpg)
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
![Page 11: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/11.jpg)
Shortening lambda syntax
Omit parameter types
Expressions instead of blocks
Single arguments? Omit parenthesis!
11
![Page 12: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/12.jpg)
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
![Page 13: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/13.jpg)
Method reference
13
![Page 14: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/14.jpg)
Core idea behind
Make lambda more succinct!
Rather than
angles.map(x -> Math.sin(x));
Can be shorthand
angles.map(Math::sin);
14
![Page 15: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/15.jpg)
Predicate
15
![Page 16: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/16.jpg)
Core idea behind
Boolean test(T t)
A function to test condition
16
![Page 17: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/17.jpg)
Benefit: Flexibility
Even better with stream (later slides)
17
![Page 18: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/18.jpg)
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
![Page 19: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/19.jpg)
More convenient helper classes
IntConsumer
IntFunction<R>
IntPredicate
IntSupplier
19
![Page 20: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/20.jpg)
Stream
20
![Page 21: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/21.jpg)
Stream
Wrapper around data sources: arrays, collections…
Use lambdas
Support map/reduce
Lazy evaluation
Made parallel atutomatically by compiler
21
![Page 22: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/22.jpg)
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
![Page 23: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/23.jpg)
Turn stream to other data structures
Array
employees.toArray(Employee[]::new);
List
names.collect(Collectors.toList());
Set
names.collect(Collectors.toSet());
23
![Page 24: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/24.jpg)
2
Comparison with LINQ in C#
![Page 25: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/25.jpg)
Restriction Operators
25
![Page 26: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/26.jpg)
1. Filtering
Find names where “am” occurs
26
![Page 27: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/27.jpg)
2. Indexed Filtering (tricky)
Find names where length <= index + 1
(generate an indexed stream out of the original array)
27
![Page 28: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/28.jpg)
Projection Operators
28
![Page 29: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/29.jpg)
3. Selecting/Mapping
Add “Hello “ in front of each names
29
![Page 30: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/30.jpg)
4. Selecting Many/Flattening
Project all the elements in a single collection
Java: Transform to entry set, then flatten
30
![Page 31: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/31.jpg)
Partitioning Operators
31
![Page 32: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/32.jpg)
5. Taking an Arbitrary Number of Items
Obtain the first 4 items
Java: convert IntStream into Stream<Integer>
32
![Page 33: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/33.jpg)
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
33Different meaning from the above!
![Page 34: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/34.jpg)
7. Skipping an Arbitrary Number of Items
Skip top items, take the rest
Java:
34
![Page 35: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/35.jpg)
8. Skipping Items Based on Predicate
LINQ: SkipWhile
Sadly, no way in Java
35
![Page 36: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/36.jpg)
Ordering Operators
36
![Page 37: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/37.jpg)
9. Ordering/Sorting Elements
Order the elements of a collection alphabetically
Java:
37
![Page 38: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/38.jpg)
10. Ordering/Sorting Elements by
Specific Criterium
Ordering by the length of the string
Java
Shorthand:
38
![Page 39: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/39.jpg)
11. Ordering/Sorting Elements by
Multiple Criteria
Sort by length, then by order
Java:
39
![Page 40: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/40.jpg)
Grouping Operators
40
![Page 41: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/41.jpg)
12.Grouping by a Criterium
Group collection of strings by their length
41
![Page 42: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/42.jpg)
Set Operators
42
![Page 43: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/43.jpg)
13. Filter Distinct Elements
Obtain all the distinct elements from a collection
43
![Page 44: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/44.jpg)
14. Union of Two Sets
Join together two sets of items
44
![Page 45: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/45.jpg)
Element Operatos
45
![Page 46: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/46.jpg)
15. First Element
Obtain the first element of a collection
Java: Maybe better!
46
![Page 47: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/47.jpg)
Range Operators
47
![Page 48: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/48.jpg)
16. Generate a Range of Numbers
Generate a range of no that are multiples of 11
48
![Page 49: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/49.jpg)
Quantifier Operators
49
![Page 50: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/50.jpg)
17. All
Do all elements in a collection satisfy a predicate?
50
![Page 51: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/51.jpg)
18. Any
Do any elements in a collection satisfy a predicate?
51
![Page 52: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/52.jpg)
Merging Operators
52
![Page 53: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/53.jpg)
19.Zip
Combine two collections into a single collection
53
![Page 54: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/54.jpg)
The last coffee drop
54
![Page 55: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/55.jpg)
Still left behind by C#! Gambatte, Java!
![Page 56: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/56.jpg)
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
![Page 57: Java 8 stream and c# 3.5](https://reader034.vdocuments.us/reader034/viewer/2022042716/55ab06d71a28abc12d8b461e/html5/thumbnails/57.jpg)
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