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

ゆるゆるJava第二回「第一回でのつまづきと復習ラムダ式」

shiryu
February 23, 2024
6

 ゆるゆるJava第二回「第一回でのつまづきと復習ラムダ式」

ゆるゆるJavaというDiscord上でのイベントのまとめです。
内容としては以下です。
- 第一回でつまづいていた内容
第一回は以下のJava21で使用できる文法でデザインパターンを新しく書いた以下のリポジトリとYoutubeを見ました。
https://github.com/yanaga/revisiting-design-patterns
https://www.youtube.com/watch?v=VCMVtodGZiA
- ラムダ式の簡単な復習

shiryu

February 23, 2024
Tweet

Transcript

  1. 該当コード public class RevisitedStrategy { public static void main(String[] args)

    { var endpoint = getEndpoint(); var passInformation = new PassInformation(new UserProfile(true, true, true)); String jwt = endpoint.get(passInformation); System.out.println(jwt); } public static GoogleWalletEndpoint getEndpoint() { var endpoint = new GoogleWalletEndpoint(); endpoint.setStrategy(pass -> "abc"); endpoint.setStrategy(new AddToGoogleWalletLink()::complete); endpoint.setStrategy(new AddToGoogleWalletLink()::preCreated); return endpoint; } } class GoogleWalletEndpoint { private Function<PassInformation, String> strategy; public String get(PassInformation passInformation) {return strategy.apply(passInformation);} public void setStrategy(Function<PassInformation, String> strategy) {this.strategy = strategy;} } 引用元 : revisiting-design-patterns
  2. 該当コードの解説 endpoint.setStrategy(pass -> "abc"); public void setStrategy(Function<PassInformation, String> strategy) {

    this.strategy = strategy; } 疑問だった点 : pass -> "abc" このpassがPassInformationの型を満たしていない…? Functionはどこに行ったのか…?
  3. 誤解1 そもそも型は要らない 公式のJava Laungage Specificationから引用 Lambda Expressions (int x) ->

    x+1 // Single declared-type parameter (int x) -> { return x+1; } // Single declared-type parameter (x) -> x+1 // Single inferred-type parameter x -> x+1 // Parentheses optional for // single inferred-type parameter
  4. 誤解2 意味がある文章だと思い込んでいた public void setStrategy(Function<PassInformation, String> strategy) { this.strategy =

    strategy; } endpoint.setStrategy(pass -> "abc"); どんなPassInformationに対しても、String"abc"を返すという意味 これ以上の意味があるのではないかと勝手に思い込んでいた
  5. ラムダ式の例1 () -> {} // No parameters; result is void

    () -> 42 // No parameters, expression body () -> null // No parameters, expression body () -> { return 42; } // No parameters, block body with return () -> { System.gc(); } // No parameters, void block body
  6. ラムダ式の例2 () -> { // Complex block body with returns

    if (true) return 12; else { int result = 15; for (int i = 1; i < 10; i++) result *= i; return result; } }
  7. ラムダ式の例3 (int x) -> x+1 // Single declared-type parameter (int

    x) -> { return x+1; } // Single declared-type parameter (x) -> x+1 // Single inferred-type parameter x -> x+1 // Parentheses optional for (String s) -> s.length() // Single declared-type parameter (Thread t) -> { t.start(); } // Single declared-type parameter s -> s.length() // Single inferred-type parameter t -> { t.start(); } // Single inferred-type parameter (int x, int y) -> x+y // Multiple declared-type parameters (x, y) -> x+y // Multiple inferred-type parameters (x, int y) -> x+y // Illegal: can't mix inferred and declared types (x, final y) -> x+y // Illegal: no modifiers with inferred types
  8. 広い意味でのラムダ式 参考 : Wikipedia ラムダ式は他の言語でも使用される。 他の言語ではクロージャと呼ばれる機能も、Javaではラムダ式として内包される。 参考 : JSR "Lambda

    expressions (informally, "closures" or "anonymous methods")" 意訳 : ラムダ式(非公式にはクロージャ・匿名メソッド) 補足 : anonymous = 匿名(自分の名前を隠して知らせないこと) 無名クラスの方が適切と言う表現もある。
  9. @zinbe(杉山さん)さんからの補足 1. 関数型インターフェースだから定数以外の参照が禁止されている訳ではない。 元々の匿名クラスのクラスファイルの後方互換性を保つための仕様 2. Virtual Threadについて StreamのParallel処理とVitual Thread、順次処理の使い分けが現状最適 virtual

    threadを生で作ったほうが早くなる可能性がある。 virtual threadは今後標準のライブラリに反映されていく 処理効率が上昇できる場合のみ バーチャルスレッドが解消するのはサーバー側のI/Oの処理のボトルネック 大量のデータはコレクションを使う方が効率が良い。 3. 宣言型について パラレルストリームは、並列処理と並行処理をJVMが判断するという点で、宣言型に近 い 4. streamAPIの表現としてラムダ式が導入されている