Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java初心者がJava8のラムダ式をやってみた

1,763 views

Published on

2015.11.15 Java女子部 Java8自由研究

Published in: Engineering
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Java初心者がJava8のラムダ式をやってみた

  1. 1. Java初心者が Java8のラムダ式をやってみた 2015.11.15 Java女子部 Java8自由研究 @aya_122 えばた あや
  2. 2. えばた あや Twitter: @aya_122 お仕事: Groovy(フレームワークはGrails)、javascript Java歴: 学校の授業でちょっとやったのと、 仕事の研修でちょっとやったくらいの初心者 好きな食べ物: ラーメン 苦手な事: 人前で発表すること← 自己紹介(´ω` )
  3. 3. 登壇するのはじめてで緊張…((((;゜Д゜)))
  4. 4. Java初心者な自分がラムダ式をやってみて 学んだ事について話します!
  5. 5. 目次 1. ラムダ式の概要 2. ラムダ式の書き方 3. おわりに
  6. 6. 1. ラムダ式の概要
  7. 7. 何故ラムダ式? 始める前: Java8なにそれ  → Java8って何があるの?  → ふむふむ…  → あ!これ、javascriptとかで似たの見た事ある!!  → ………クロージャか!!!!!!!   (実際違うとも言われるけど←) ってなわけで、 一番身近に感じたのでラムダ式を選びました(^ω^) 選んだ理由 とか(^ω^)
  8. 8. Java8で導入された新たな表記法 ・Java8で導入された新機能と組み合わせて使うのに最適 ・関数型インターフェース ・特に、Stream API ラムダ式とは
  9. 9. Java8で導入された新たな表記法 ・Java8で導入された新機能と組み合わせて使うのに最適 ・関数型インターフェース ・特に、Stream API ラムダ式とは 記述の簡素化
  10. 10. 2. ラムダ式の書き方
  11. 11. 書き方 ラムダ式の基本的な書き方として、 ( 引数 ) -> { 処理 } と表します。
  12. 12. 引数部分の書き方 引数部分についての書き方の基本形は、 ・引数が0個の場合 ( ) -> { 処理 } ・引数が1個の場合 ( int n ) -> { 処理 } ・引数が2個の場合 ( String s, int n ) -> { 処理 } のように表します。
  13. 13. 引数部分の書き方 ・引数の型を推論することが出来る場合、  型を省略することが可能 ( int n ) -> { 処理 } ( n ) -> { 処理 } ・引数が1個の場合、カッコを省略することが可能 ( n ) -> { 処理 } n -> { 処理 } 省略可能!! 省略 省略
  14. 14. 処理部分の書き方 処理部分についての書き方の基本形は、 ( 引数 ) -> { 何か処理; return 戻り値; } のように表します。 もちろん、処理部分でも省略が可能です!!
  15. 15. 処理部分の書き方 ・戻り値がvoid、または処理が1個の場合、  returnを省略することが可能 ( 引数 ) -> { 何か処理; } ・処理が1個しかない場合、カッコを省略することが可能 ( 引数 ) -> 処理 こっちも省略 可能(´ω` )
  16. 16. ラムダ式を書いてみる 実際に、引数部分と処理部分に値を入れて書いてみると、 ( String s ) -> { System.out.println( “string: ” + s ); }
  17. 17. でも…
  18. 18. どうせなら、 さっき説明した省略した書き方 使いたいよね(´∀`)
  19. 19. 省略して書いてみる 引数部分で一番簡単な書き方と、 処理部分で一番簡単な書き方を合わせて書くと、 s -> System.out.println(“string: ” + s) こんなにシンプルに書けます★ (処理が2つ以上の場合は、{ }つける書き方で!) じゃじゃん! ( `ω´ )
  20. 20. 実際に使ってみる その前に!以前の書き方で書いてみると… List<String> food = new ArrayList<String>(); food.add("アイス"); food.add("ラーメン"); food.add("もつ鍋"); for ( String s : food ) { System.out.println(“好きな食べ物: ” + s); } <出力結果> 好きな食べ物: アイス 好きな食べ物: ラーメン 好きな食べ物: もつ鍋 Listの値を 1つずつ出力
  21. 21. 実際に使ってみる ラムダ式を使うと!! List<String> food = new ArrayList<String>(); food.add("アイス"); food.add("ラーメン"); food.add("もつ鍋"); food.forEach(s -> System.out.println("好きな食べ物: " + s)); Java8な 書き方(‾∀‾ ) <出力結果> 好きな食べ物: アイス 好きな食べ物: ラーメン 好きな食べ物: もつ鍋
  22. 22. 注意点 int n = 1; Runnable runner = () -> { n++; }; runner.run(); ちなみに、このように書くと…
  23. 23. 注意点 int n = 1; Runnable runner = () -> { n++; }; runner.run(); コンパイル エラー ラムダ式の外側で定義された変数に、 ラムダ式の内側から値を代入すると コンパイルエラー
  24. 24. 3. おわりに
  25. 25. 感想 ・つい最近までJavaが8になってたなんて知らなかった  けど、これを機会にJava8について知ることができた ・ラムダ式は他のJava8の機能と相性が良いので、他の  Java8の機能といろいろ組み合わせてもっともっと使って  みたくなった
  26. 26. ありがとうございました(^ω^)
  27. 27. と、見せかけて。
  28. 28. 時間が余ったので、
  29. 29. 問題だします!!!!!
  30. 30. 問題 この中でコンパイルエラーにならないものはどれでしょう? 1. Object object = () -> System.out.println("abc"); 2. int n = 1;  Runnable runner = () -> System.out.println(n);  n++;  runner.run(); 選択肢、続く…
  31. 31. 問題 続き… 3. List<String> list = new ArrayList<>();  list.add("a");  list.add("b");  list.forEach(_ -> System.out.println(_)); 4. Runnable runner = () -> System.out.println(this);  runner.run();
  32. 32. 答え
  33. 33. 答え 正解は、 4. Runnable runner = () -> System.out.println(this);  runner.run(); です! Runnable runner = () -> System.out.println(toString()); runner.run(); と同じ出力結果が得られる
  34. 34. 解説 1. Object object = () -> System.out.println("abc"); ラムダ式がどの関数型インターフェースのものなのか 分からない為、コンパイルエラー Object object = (Runnable)() -> System.out.println("abc"); こう書くことでエラーから抜け出せます。
  35. 35. 解説 2. int n = 1;  Runnable runner = () -> System.out.println(n);  n++;  runner.run(); 変数に再代入する場所がラムダ式の外でもコンパイルエラー
  36. 36. 解説 3. List<String> list = new ArrayList<>();  list.add("a");  list.add("b");  list.forEach(_ -> System.out.println(_)); ラムダ式の引数名には、アンダーバーのみのものが使えない ため、コンパイルエラー → 文字列等を使うこと
  37. 37. 今度こそ、
  38. 38. ありがとうございました(^ω^)

×