LoginSignup
1
2

More than 5 years have passed since last update.

FunctionalInterfaceがスッキリできたサンプル

Posted at

Labmda式とFunctionalInterfaceが
私の中でモヤッとしてたのですが、
この実装を見てスッキリしたので、サンプルをメモしておきます。

public class FunctinalInterfaceSmaple {

    // FunctionalInterfaceを宣言 
    @FunctionalInterface
    interface sampleInterface {
        public String joinString(String a, String b);
    }

    // 実際のメイン処理
    public static void main(String[] args) {

        // lambda式を返すMap
        Map<String, sampleInterface> sampleMap = new HashMap<>();
        sampleMap.put("join", (a, b) -> a + b);
        sampleMap.put("join2", (a, b) -> a + b + a + b);

        String firstString = "aaaa";
        String secondString = "bbbb";

        sampleInterface mySampleInterface = sampleMap.get("join");
        sampleInterface mySampleInterface2 = sampleMap.get("join2");

        System.out.println(mySampleInterface.joinString(firstString, secondString));
        System.out.println(mySampleInterface2.joinString(firstString, secondString));

    }
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2