2
Disclaimer
2. This slide powered by reveal.js and JavaFX.
3. I don't know Java in Republik Indonesia.
4. This slide licensed .
his slide's content is mere my opinion.
Eclipse Public License Version 1.0
Slide 3
Slide 3 text
3
Who are you?
Slide 4
Slide 4 text
4
Who are you?
My name is "Toast kid".
I work on Web service company.
I like GUI tool.
I often use Java SE on work.
Storm / Play(1.x) / Spark Framework / Android App
8
Target
1. The Java programmer who has not yet used Java8's
API.
2. The Android app developper who has not yet used
Java8's API.
3. The Java programmer who feels strange to Lambda
expression.
4. The programmer who think that Stream API is not
useful because for loop is so fantastic.
Slide 9
Slide 9 text
9
Goal
1. You want to rewrite your codes with Java8 like.
2. You become forloop prohibition debater.
Slide 10
Slide 10 text
10
Overview
1. Stream API
2. Conclusion
Slide 11
Slide 11 text
11
Stream API
Everything is a stream.
Slide 12
Slide 12 text
12
About Stream API
For internal iteration
We often use it for operating Collection.
Slide 13
Slide 13 text
13
Stream API's melit for Java
developer
1. It can use new Collection operator API.
2. You can write Java source code like natural language.
3. You can parallel processing easily.
Slide 14
Slide 14 text
14
Stream API's Operators
You should learn by heart only 3 operators.
Slide 15
Slide 15 text
15
filter
Slide 16
Slide 16 text
16
map
Slide 17
Slide 17 text
17
forEach
Slide 18
Slide 18 text
18
operator samples
filter select elements
map convert elements
forEach operate all elements
It's similar to RxJava's operators.
23
written by former API.
final String[] nums = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
for (final String n : nums) {
final int i = Integer.parseInt(n);
if (i % 2 == 0) {
System.out.println(i);
}
}
Slide 24
Slide 24 text
24
Sample
Slide 25
Slide 25 text
25
Stream API Example
Q.全社員のうち、65歳以上の社員が所属するグループの名
前を出力せよ
Slide 26
Slide 26 text
26
Before
Q.全社員のうち、65歳以上の社員が所属するグループの名
前を出力せよ
List employees = ...;
Set groups = new HashSet<>();
for (Person p : people) {
if (p.getAge() >= 65) {
groups.add(p.getGroup());
}
}
for (Group g : groups) {
System.out.println(g.getName());
}
Oh...Too complex!
31
To concurrent with Lambda &
Stream API
employees
.parallelStream() // add
.filter(p > {return p.getAge() >= 65;})
.map(p > {return p.getGroup();})
.sequential() // add
.forEach(g > {System.out.println(g.getName());});
Slide 32
Slide 32 text
32
Problem of Stream API
1. It can't replace all loop code.
2. It's not enough functions.
1. Why don't you use Eclipse Collections!
3. It use more memory than nomal loop code.
4. It can't debug each line.
Slide 33
Slide 33 text
33
Conclusion
Slide 34
Slide 34 text
34
Conclusion
1. Stream API をとりあえず使うには filter/map/forEach だ
け覚えればなんとかなるよ
2. Stream API 使うと Collection 操作のコードが読みやすく
なるよ
3. 標準の Stream API に力不足を感じたら Eclipse
Collections を使おう
Slide 35
Slide 35 text
35
Do you feel it so strange?
変化には必ずコストが伴います。(中略)し
かし、Project Lambda による生産性と表
現力の向上は、これらのコストを補って余
りあるはずです
出典:『Java Magazine vol.7』Project Lambda の展望 言
語アーキテクトのBrian Goetzに聞く(p35)