Upgrade to Pro — share decks privately, control downloads, hide ads and more …

意識低いプログラマーのJava8入門

Toast kid
April 14, 2016

 意識低いプログラマーのJava8入門

本番までに差し替える可能性あり
まだ Java SE 8 を使ったことない人向け

Toast kid

April 14, 2016
Tweet

More Decks by Toast kid

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 6 Qiita Articles @y_q1m 2015/11/29 2016/01/09 2016/04/07 2016/04/09 Eclipse Collections

    に挑む Java の Pair について考える JavaFX でワードクラウドを作る 声に出して読みたい Java ライブラリ ­filter­
  4. 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.
  5. 9 Goal 1. You want to rewrite your codes with

    Java8 like. 2. You become for­loop prohibition debater.
  6. 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.
  7. 18 operator samples filter select elements map convert elements forEach

    operate all elements It's similar to RxJava's operators.
  8. 20 map Stream.of("0", "1", "2", "3", "4", "5", "6", "7",

    "8", "9") .map(i ­> {return Integer.parseInt(i);})
  9. 22 combined operators. Stream.of("0", "1", "2", "3", "4", "5", "6",

    "7", "8", "9") .map(i ­> {return Integer.parseInt(i);}) .filter(i ­> {return i % 2 == 0;}) .forEach(i ­> {System.out.println(i);});
  10. 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); } }
  11. 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!
  12. 29 What's good? 1. 余分な変数がない 2. 上から下に読めば何をしたいか明確 3. 必要であればメソッドに切り出せる……テストしやすくなる employees

    .filter(p ­> {return p.getAge() >= 65;}) .map(p ­> {return p.getGroup();}) .forEach(g ­> {System.out.println(g.getName());});
  13. 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());});
  14. 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.
  15. 34 Conclusion 1. Stream API をとりあえず使うには filter/map/forEach だ け覚えればなんとかなるよ 2.

    Stream API 使うと Collection 操作のコードが読みやすく なるよ 3. 標準の Stream API に力不足を感じたら Eclipse Collections を使おう
  16. 35 Do you feel it so strange? 変化には必ずコストが伴います。(中略)し かし、Project Lambda

    による生産性と表 現力の向上は、これらのコストを補って余 りあるはずです 出典:『Java Magazine vol.7』Project Lambda の展望 ­ 言 語アーキテクトのBrian Goetzに聞く(p35­)
  17. 36 References 1. 現場で使える 最新 Java SE 7/8 速攻入門 櫻庭

    祐一 (著) / 技術評論社 (2015/11/19) 2. Java SE 8のラムダ式はどう実現されたのか?──実装の 経緯、内部的な仕組みを理解する http://builder.japan.zdnet.com/sp_oracle/weblogic/35077771