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
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
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
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
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
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]
Đố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.*
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.