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

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おすすめブログ