Transcript
Page 1: Java 8 stream and c# 3.5

Java 8 Stream & C# 3.5

Trần Duy Quang – May 2014

Page 2: Java 8 stream and c# 3.5

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

1

Some new things in Java 8

Page 4: Java 8 stream and c# 3.5

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

Lambda expression

5

(x, y) -> x + y

Page 6: Java 8 stream and c# 3.5

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

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

Main advantage of lambdas

Concise & expressive

8

Page 9: Java 8 stream and c# 3.5

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

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

Shortening lambda syntax

Omit parameter types

Expressions instead of blocks

Single arguments? Omit parenthesis!

11

Page 12: Java 8 stream and c# 3.5

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

Method reference

13

Page 14: Java 8 stream and c# 3.5

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

Predicate

15

Page 16: Java 8 stream and c# 3.5

Core idea behind

Boolean test(T t)

A function to test condition

16

Page 17: Java 8 stream and c# 3.5

Benefit: Flexibility

Even better with stream (later slides)

17

Page 18: Java 8 stream and c# 3.5

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

More convenient helper classes

IntConsumer

IntFunction<R>

IntPredicate

IntSupplier

19

Page 20: Java 8 stream and c# 3.5

Stream

20

Page 21: Java 8 stream and c# 3.5

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

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

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

2

Comparison with LINQ in C#

Page 25: Java 8 stream and c# 3.5

Restriction Operators

25

Page 26: Java 8 stream and c# 3.5

1. Filtering

Find names where “am” occurs

26

Page 27: Java 8 stream and c# 3.5

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

Projection Operators

28

Page 29: Java 8 stream and c# 3.5

3. Selecting/Mapping

Add “Hello “ in front of each names

29

Page 30: Java 8 stream and c# 3.5

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

Partitioning Operators

31

Page 32: Java 8 stream and c# 3.5

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

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

7. Skipping an Arbitrary Number of Items

Skip top items, take the rest

Java:

34

Page 35: Java 8 stream and c# 3.5

8. Skipping Items Based on Predicate

LINQ: SkipWhile

Sadly, no way in Java

35

Page 36: Java 8 stream and c# 3.5

Ordering Operators

36

Page 37: Java 8 stream and c# 3.5

9. Ordering/Sorting Elements

Order the elements of a collection alphabetically

Java:

37

Page 38: Java 8 stream and c# 3.5

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

11. Ordering/Sorting Elements by

Multiple Criteria

Sort by length, then by order

Java:

39

Page 40: Java 8 stream and c# 3.5

Grouping Operators

40

Page 41: Java 8 stream and c# 3.5

12.Grouping by a Criterium

Group collection of strings by their length

41

Page 42: Java 8 stream and c# 3.5

Set Operators

42

Page 43: Java 8 stream and c# 3.5

13. Filter Distinct Elements

Obtain all the distinct elements from a collection

43

Page 44: Java 8 stream and c# 3.5

14. Union of Two Sets

Join together two sets of items

44

Page 45: Java 8 stream and c# 3.5

Element Operatos

45

Page 46: Java 8 stream and c# 3.5

15. First Element

Obtain the first element of a collection

Java: Maybe better!

46

Page 47: Java 8 stream and c# 3.5

Range Operators

47

Page 48: Java 8 stream and c# 3.5

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

Quantifier Operators

49

Page 50: Java 8 stream and c# 3.5

17. All

Do all elements in a collection satisfy a predicate?

50

Page 51: Java 8 stream and c# 3.5

18. Any

Do any elements in a collection satisfy a predicate?

51

Page 52: Java 8 stream and c# 3.5

Merging Operators

52

Page 53: Java 8 stream and c# 3.5

19.Zip

Combine two collections into a single collection

53

Page 54: Java 8 stream and c# 3.5

The last coffee drop

54

Page 55: Java 8 stream and c# 3.5

Still left behind by C#! Gambatte, Java!

Page 56: Java 8 stream and c# 3.5

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


Top Related