Skip to Content
Javaへの関数的アプローチ
book

Javaへの関数的アプローチ

by Ben Weidig
March 2025
Intermediate to advanced
414 pages
5h 47m
Japanese
O'Reilly Media, Inc.
Content preview from Javaへの関数的アプローチ

第2章. 関数型Java

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

当然のことながら、Lambda式はJavaで関数的なアプローチをするための鍵となる。

この章では、JavaでLambdaを使う方法、Lambdaがなぜ重要なのか、Lambdaを効率的に使う方法、Lambdaが内部でどのように動作するのかを学ぶ。

Java Lambdaとは何か?

Lambda式とは、0個以上のパラメータを持ち、戻り値を持つ可能性のある、Javaコードの1行またはブロックのことである。 簡略化すると、Lambdaは、どのオブジェクトにも属さない匿名メソッドのようなものである:

() -> System.out.println("Hello, lambda!")

構文の詳細と、JavaでLambdaがどのように実装されているかを見てみよう。

Lambda構文

JavaのLambdaの構文は、第1章で見たラムダ計算の数学的表記法とよく似ている:

(<parameters>) -> { <body> }

シンタックスは3つの異なる部分で構成されている:

パラメータ

メソッドの引数リストと同じように、カンマで区切られたパラメータのリスト。 ただし、メソッドの引数とは異なり、コンパイラが引数の型を推測できる場合は、引数の型を省略することができる。 暗黙的に型付けされたパラメータと明示的に型付けされたパラメータを混在させることはできない。 パラメータが1つの場合は括弧は必要ないが、パラメータが1つもないか複数存在する場合は括弧が必要である。

アロー

-> (矢印)は、パラメータとLambda本体を分離するものである。 λ に相当する。

ボディ

単一行式またはコードブロック。単一行式には波括弧は必要なく、評価された結果はreturn 文なしで暗黙的に返される。 本体が単一行式以上で表される場合は、典型的なJavaコードブロックを使用する。値が返される場合は、波括弧で囲み、return 文を明示的に使用する必要がある。

これがJavaにおけるラムダの定義である。 ラムダを宣言する方法が複数あるため、例2-1のように、同じラムダを異なる冗長度で書くことができる。

例2-1. 同じLambdaの異なる書き方
(String input) -> { 1
  return input != null;
}

input -> { 2
  return input != null;
}

(String input) -> input != null 3

input -> input != null 

最も冗長なバリエーション:括弧の中に明示的に型付けされたパラメータとボディブロックがある。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学習アルゴリズム

学習アルゴリズム

George Heineman
エンジニアが学ぶ会計システムの「知識」と「技術」

エンジニアが学ぶ会計システムの「知識」と「技術」

広川 敬祐, 五島 伸二, 小田 恭彦, 大塚 晃, 川勝 健司

Publisher Resources

ISBN: 9798341625730