This talk covers Java 8 Streams in details and using programming puzzles will discuss about various techniques with Streams: how to do complex data processing , queries with Streams API, how to use all the goodies like map, flatMap,filter, how to group, and partition data, how to use infinite streams, parallel streams, creating lazy lists.
If you are a beginner or a pro with Java 8 wanting to challenge your knowledge there is something for all!
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
Let if flow: Java 8 Streams puzzles and more
1. Let it flow Java
8 stream puzzles
and more
Bhakti Mehta
@bhakti_mehta
2. Introduction
O Senior Software Engineer at Blue Jeans
Network
O Worked at Sun Microsystems/Oracle for 13
years
O Committer to numerous open source projects
including GlassFish Application Server
6. Evolution
of
Java
O Java 8 has myriad of features
O Most prominent are lamdas and streams API
O Functional style to Java
7. Streams
O Abstraction not a datastructure
O Can transform data
O Value in motion
O Functional style will affect all collections
O Automatic parallelism
9. Streams
in
Java
8
Computed on demand
Just in time
Lazily constructed collection
10. Stream
or
loop
GET the shortest list of cities of people
Instead of
List people = …
Set shortCities = new HashSet<>();
for (Person p : people) {
City c = p.getCity();
if (c.getName().length() < 5 ) {
shortCities.add(c);
}
11. Stream
or
loop
We write
List people = …
Set shortCities = people.stream()
.map(Person::getCity)
.filter(c -> c.getName().length() < 5)
.collect(toSet());
12. Stream
or
loop
We write
List people = …
Set shortCities = people.stream()
.map(Person::getCity)
.filter(c -> c.getName().length() < 5)
.collect(toSet());
More
concise
More
readableComposable
operationsCan be made
parallel
13. Power
of
Stream
O Streams provide the power to write compose
functions and data flows through the
functions
21. Quiz
Given a list of numbers return the Squares
Given [1,2,3,4] return [1,4,9,16]
22. Quiz
Given a list of numbers return the Squares
Given [1,2,3,4] return [1,4,9,16]
List<Integer>numbers = Arrays.asList(1,2,3,4);
List<Integer> squares = numbers.stream().map
(n->n*n).collect(toList());
32. Quiz
Is this valid sample?
menu.stream().collect
(partitioningBy
(Item::isSeaFood()
,partitioningBy
(d->d.getCalories()>500)));
33. Quiz
Is this valid sample?
menu.stream().collect
(partitioningBy
(Item::isSeaFood()
,partitioningBy
(d->d.getCalories()>500)));
34. Composability
O Get the unique surnames in uppercase of the
first 15 book authors that are 50 years old
or older
O library.stream() .map(book ->
book.getAuthor()) .filter(author ->
author.getAge() >=
50) .limit(15) .map(Author::getSurname) .ma
p(String::toUpperCase) .distinct() .collect(toLi
st()));
36. Parallel
streams
O Parallel streams = simple concurrency
O Parallel stream splits its elements in multiple
chunks processing each chunk on a
different thread
O Potentially can use N cores => Nx
speedup
37. Parallel
Streams
O Check the benchmark using sequential and
parallel streams
O Limit and findFirst may be expensive in
parallel stream as they rely on the odrder of
the elements. Use findAny if you are not
constrained by the order