Skip to Content
学习 Java,第 6 版
book

学习 Java,第 6 版

by Marc Loy, Patrick Niemeyer, Daniel Leuck
May 2025
Intermediate to advanced
552 pages
7h 22m
Chinese
O'Reilly Media, Inc.
Content preview from 学习 Java,第 6 版

第 11 章 Java 中的函数式方法 Java 中的函数式方法

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

Java 是一种面向对象的语言,而且仍然是一种面向对象的语言。我们在第 5 章中看到的所有设计模式和类类型仍然是大多数开发人员编写 Java 代码的核心。Java 也很灵活,个人和企业都可以提出改进建议并加以改进。随着函数式编程(FP)重回人们的视线,Java 也跟上了步伐。FP 代表了另一种编程方法:重点是函数而不是对象。

早在 Java 8 开始,Java 就通过java.util.function 包支持了一套合理的函数式功能。该包包含多个类和接口,允许开发人员使用流行的函数式方法来解决问题。我们将在本章中探讨其中的一些方法,但我们要强调的是动词 "允许"。如果你不喜欢函数式编程,你可以放心地忽略本章。不过我们希望你能尝试一些示例。有一些不错的特性可以让你的代码更紧凑,同时保持其可读性。

功能 101

函数式编程的起源可以追溯到 20 世纪 30 年代,美国数学家阿伦佐-丘奇(Alonzo Church)和他的 Lambda 微积分。丘奇并没有在任何硬件上运行他的微积分,但 Lambda 微积分正式确立了一种解决问题的方式,并由此产生了为真正的操作机器编写的早期编程语言。1Lisp 语言是 20 世纪 50 年代在麻省理工学院开发的,可以在 IBM 700 系列等早期迭代的现代计算机上运行。如果你能想象出一张黑白老照片上书架大小的闪烁灯墙,你就能正确理解 FP 思想和模式在计算史上的久远历史。

但FP 并非计算机编程的唯一方式。其他范式,如过程编程和面向对象编程(OOP),也经常争相流行。令人欣慰的是,你可以用其中任何一种范式实现相同的目标。选择哪种范式通常取决于问题领域和个人偏好。

考虑一下将两个数字相加并将结果赋值给一个变量的简单任务。我们可以用面向对象的语言(如 Java)、函数式语言(如 Clojure)或过程式语言(如 C 语言)来完成这项任务:".....、2或者像 C 语言这样的过程式语言:

// Java objects
BigInteger five = new BigInteger(5);
BigInteger twelve = new BigInteger(12);
BigInteger sum = five.add(twelve);

// Clojure
(def five 5)
(def twelve 12)
(def sum (+ five twelve))

// C
int five = 5;
int twelve = 12;
int sum = five + twelve;

Java 是在 OOP(再次)兴起时进入数字领域的,它反映了这些根源。尽管如此,函数式编程一直都有自己的传道者。Java 8 对语言进行了一些实质性的补充,为函数式编程的爱好者打开了使用 Java 的大门。让我们来看看其中的一些新增功能,看看它们是如何与更广阔的 Java 世界相融合的。

Lambda

受lambda calculus 的启发,lambda 表达式(或更简单地称为 lambdas)构成了 Java 函数式编程的核心单元。Lambda 是一种封装逻辑的方法。在函数式语言中,函数是 "一等公民",可以像 Java 中的对象一样被创建、存储、引用、使用和传递。为了模仿这种功能,Java 8 引入了一些新语法和几个特殊接口。这些新增功能允许您快速定义一个可以替代整个内部类的函数。当然,定义的结果仍然是一个隐藏的对象,只是其 ...

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

《学习 Python》第 5 版

《学习 Python》第 5 版

Mark Lutz
CSS:权威指南,第 5 版

CSS:权威指南,第 5 版

Eric Meyer, Estelle Weyl
詳解 システム・パフォーマンス 第2版

詳解 システム・パフォーマンス 第2版

Brendan Gregg, 西脇 靖紘, 長尾 高弘

Publisher Resources

ISBN: 9798341656772