【译】【原文翻译计划】Java Optional的flatMap方法
2022/1/30 20:04:31
本文主要是介绍【译】【原文翻译计划】Java Optional的flatMap方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
原文节选自《java 8 in Action》, 297页:
原文如下:
Here you begin with the optional wrapping the Person and invoking flatMap(Person::getCar)on
it. As we said, you can logically think of this invocation as something that happens in two steps.
In step 1, a Function is applied to the Person inside the optional to transform it. In this case, the
Function is expressed with a method reference invoking the method getCar on that Person.
Because that method returns an Optional
into an instance of that type, resulting in a two-level optional that’s flattened as part of the
flatMap operation. From a theoretical point of view, you can think of this flattening operation as
the operation that combines two optionals, resulting in an empty optional, if at least one of them
is empty. What happens in reality is that if you invoke flatMap on an empty optional, nothing is
changed, and it’s returned as is. Conversely, if the optional wraps a Person, the Function passed
to the flatMap method is applied to that Person. Because the value produced by that Function
application is already an optional, the flatMap method can return it as is.
译文:
这里从包装一个Person对象的optional对象开始,对其调用flatMap(Person::getCar)方法。正如我们前面说的,你逻辑上可以认为这个调用包含两步:第一步,一个Function被应用在Optional对象中的Person对象上,其目的是把Person对象转换成一个其他的对象。在这里,这个Function就是用Person对象的getCar方法。由于这个方法返回一个Optional
这篇关于【译】【原文翻译计划】Java Optional的flatMap方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南