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

第6章 ストリームを使ったデータ処理 ストリームを使ったデータ処理

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

ほとんどのプログラミング言語では、データを処理する必要がある。 命令型アプローチでは、ループを使って要素を繰り返し処理し、各要素を順番に処理する。 しかし、関数型言語では宣言型アプローチが好まれ、古典的なループ文がないこともある。

Java 8で導入されたStreams APIは、完全な宣言型かつ遅延評価型のデータ処理アプローチを提供し、ほとんどの演算順序に高階関数を利用することで、Javaの関数型追加機能の利点を享受できる。

この章では、命令型データ処理と宣言型データ処理の違いについて学ぶ。 その後、ストリームの基本概念を視覚的に紹介し、データ処理により機能的なアプローチを実現するために、ストリームの柔軟性を最大限に活用する方法を紹介する。

反復によるデータ処理

データを処理することは、おそらくこれまで何百万回も遭遇してきたし、今後も遭遇し続けるであろう日常的な作業だ。

大まかな見方をすれば、どのようなタイプのデータ処理もパイプラインのように機能する。Collectionのようなデータ構造が要素を提供し、フィルタリングや要素の変換のような1つ以上の演算子があり、最後に何らかの形で結果を提供する。 結果は別のデータ構造かもしれないし、別のタスクを実行するためにそれを使うかもしれない。

簡単なデータ処理の例から始めよう。

外部反復

たとえば、Book インスタンスのリストから、タイトル順に並べられた1970年以前の3冊のSF本を発見する必要があるとしよう。例6-1は、for-ループを使った典型的な命令形アプローチを使った方法を示している。

例6-1. for-ループで本を発見する。
record Book(String title, int year, Genre genre) {
  // NO BODY
}

// DATA PREPARATION

List<Book> books = ...; 1

Collections.sort(books, Comparator.comparing(Book::title)); 2

// FOR-LOOP

List<String> result = new ArrayList<>();

for (var book : books) {
    if (book.year() >= 1970) { 3
        continue;
    }

    if (book.genre() != Genre.SCIENCE_FICTION) { 
        continue;
    }

    var title = book.title(); 
    result ...
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