干温厩温のラムダ塀ってなんだ��

n-ozawan

峻さん、こんにちは。晦永蝕�kグル�`プの稼-看噛温敬温稼です。
クリスマスですね。トナカイの映が橿いのは、掲械に謹くの谷聾僮砿が鹿まっており、��の梁業距准をしているそうです。

云籾です。
JavaのStream APIなどでよく��かける() -> {...}のような慕き圭、そういう麗だと房って措く尖盾せずに聞ってませんか�燭海譴牢害恒慌�8で擬秘されたラムダ塀と柵ばれる慕き圭で、書指はこのラムダ塀がどういうものかを盾�hします。

ラムダ塀

ラムダ塀とは��

ラムダ塀は、�v方侏インタ�`フェ�`スの渇�鵐瓮愁奪匹鯆簔�で�g廾するための芝隈です。佃しいですね。乏を弖って�h苧します。

干温厩温はオブジェクト峺�鰕墫錣任△蝓�圻夸、クラスを�g廾する駅勣があります。ラムダ塀が鞠魁する參念は、メソッド1つ協吶するにしても、駅ずクラスを協吶していました。參和は涙兆クラスを聞って協吶したコ�`ドです。�g廾も中宜ですし、採よりも辛�i來が掲械に��いです。

Runnable runner = new Runnable() {
  @Override
  public void run() {
    System.out.println("Hello, World!");
  }
};

runner.run();

そこで鞠魁したのがラムダ塀でした。ラムダ塀であれば參和のような芝峰ができます。

Runnable runner = () -> System.out.println("Hello, World!");
runner.run();

これは、これまで芝峰していたクラスを酒待晒したものになります。�I尖坪何ではクラスが贋壓することに廣吭が駅勣です。

�v方侏インタ�`フェ�`スとは��

�v方侏インタ�`フェ�`スとは、渇�鵐瓮愁奪匹�1つだけ隔つインタ�`フェ�`スのことです。ラムダ塀は、渇�鵐瓮愁奪匹�1つだけ隔つインタ�`フェ�`スでのみ、酒待晒した芝峰ができるようになります。

interface FunctionalInterfaceExample {
  void run(String message);
}

FunctionalInterfaceExample example = (message) -> System.out.println(message);
example.run("Hello, Functional Interface!");

もし、渇�鵐瓮愁奪匹�2つ參貧ある魁栽、ビルド扮にエラ�`になります。

interface FunctionalInterfaceExample {
  void run(String message);
  void hoge();
}

FunctionalInterfaceExample example = (message) -> System.out.println(message);
example.run("Hello, Functional Interface!");

// main.java:24: error: incompatible types: FunctionalInterfaceExample is not a functional interface
//     FunctionalInterfaceExample example = (message) -> {
//                                          ^
//     multiple non-overriding abstract methods found in interface FunctionalInterfaceExample
// 1 error
// error: compilation failed

�v方侏インタ�`フェ�`スを協吶する魁栽は、@FunctionalInterfaceあのテ�`ションを原けて、苧幣議に�v方侏インタ�`フェ�`スであることを幣すのが匯違議です。

@FunctionalInterface
interface FunctionalInterfaceExample {
  void run(String message);
}

炎�覆礼v方侏インタ�`フェ�`ス

いちいち�v方侏インタ�`フェ�`スを協吶するまでもない、その魁�泙蠅��I尖をラムダ塀で芝峰したい魁栽があります。干温厩温は、��喘議に聞える�v方侏インタ�`フェ�`スを喘吭しています。

Function<T,R>

Function<T,R>は、1つの哈方と卦抜�、魍屬堤v方侏インタ�`フェ�`スです。匯違議に鞭け函った�、��篁擦靴瞳気��I尖などに聞われます。

Function<String, Integer> stringLengthFunction = (str) -> str.length() * 10;
int length = stringLengthFunction.apply("Hello");
System.out.println("Length: " + length);    // Outputs: Length: 50

Consumer<T>

Consumer<T>は、1つの哈方を鞭け函り、卦抜�、魍屬燭覆らv方侏インタ�`フェ�`スです。匯違議に鞭け函った�、��I尖などに聞われます。

Consumer<String> printer = (message) -> System.out.println(message);
printer.accept("Hello, Consumer!");     // Outputs: Hello, Consumer!

Predicate<T>

Predicate<T>は、1の哈方を鞭け函り、恢看看鉛艶温稼を卦抜する�v方侏インタ�`フェ�`スです。匯違議に鞭け函った�、���して登協を佩う�I尖などに聞われます。

Predicate<String> isHello = (str) -> str != null && str.equals("hello");
System.out.println(isHello.test("hello"));  // Outputs: true
System.out.println(isHello.test(null));     // Outputs: false

Supplier<T>

Supplier<T>は、哈方なしで、採かしらの潤惚を卦抜する�v方侏インタ�`フェ�`スです。匯違議に岱方�、良�撹や兜豚晒�I尖などに聞われます。

Supplier<Double> randomSupplier = () -> Math.random();
System.out.println("Random number: " + randomSupplier.get());

まとめ

ラムダ塀は、�v方侏インタ�`フェ�`スを酒準に�g廾し、尅る玲いを�、箸靴��Qうための膿薦な字嬬です。ラムダ塀を聞いこなすことで、コ�`ドがシンプルになり、隠便來も�鯢呂靴泙后�

ではまた。


Recommendおすすめブログ