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への関数的アプローチ

第3章 JDKの機能インターフェイス JDKの機能インタフェース

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

多くの関数型プログラミング言語では、引数や戻り値の型、実際の使用ケースに関係なく、ラムダを記述するために「関数」という単一かつ動的な概念のみを使用している。 一方、Javaはラムダを含むすべてのものに有形型を必要とする厳格な型付き言語である。 そのためJDKでは、関数型ツールセットを開始するための40以上の関数型インタフェースをjava.util.function

この章では、最も重要な機能的インタフェースを紹介し、なぜこれほど多くのバリエーションがあるのかを説明し、自分のコードをより機能的に拡張する方法を紹介する。

機能的インタフェースの4大カテゴリー

java.util.function 、40以上の機能的インタフェースは4つのメイン関数に分類され、各カテゴリは重要な機能的ユースケースを表している:

  • 関数は引数を受け取り、結果を返す。

  • コンシューマは引数だけを受け取り、結果を返さない。

  • サプライヤーは引数を受け付けず、結果のみを返す。

  • 述語は引数を受け取って式と照合し、その結果としてboolean プリミティブを返す。

これら4つのカテゴリーは、多くのユースケースをカバーしており、その名前付けは機能的インターフェースのタイプとそのバリエーションに関連している。

機能的インタフェースの4つのメインカテゴリーを見てみよう。

関数

java.util.function.Function<T, R> 図3-1に見られるように、関数は単一の入力と出力を持つ "古典的な "関数である。

Function<T, R>
図3-1. java.util.function.Function<T, R>

Function<T, R> の単一の抽象メソッドはapply と呼ばれ、T 型の引数を受け入れ、R 型の結果を生成する:

@FunctionalInterface
public interface Function<T, R> {
  R apply(T t);
}

以下のコードは、null をチェックし、StringInteger としてその長さに変換する方法を示している:

Function<String, Integer> stringLength = str -> str != null ? str.length() : 0;

Integer result = stringLength.apply("Hello, Function!");

入力タイプT と出力タイプR は同一でも構わない。しかし、「関数のアリティ」では、同一のタイプを持つ特殊化された機能インタフェースのバリエーションについて述べている。

コンシューマ

図3-2に示すように、コンシューマは入力パラメータを消費するだけで、何も返さない。中心的なコンシューマの機能インタフェースはjava.util.function.Consumer<T> である。

図3-2. java.util.function.Consumer<T>

Consumer<T> の単一の抽象メソッドはaccept と呼ばれ、 ...

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