深入了解Java 8中的可选类API
作为Java程序员,我们所有人都经历了以下情况:我们调用一个方法来获取某个值,然后代替直接对返回值调用某些方法,我们首先必须检查返回值不为null,然后在返回值。 这是像Guava这样的外部API试图解决的难题 。 此外,其他JVM语言(例如Scala,Ceylon等)也将这些功能嵌入了核心API。 在我以前的文章中,我写了关于一种这样的JVM语言即Scala的支持的文章 。
Java的较新版本(即Java 8)引入了一个名为Optional的新类。 可选类的Javadoc说:
可以包含或不包含非null值的容器对象。 如果存在值,则isPresent()将返回true,而get()将返回该值。
在这篇文章中,让我们看一下Optional类中存在的每个方法,并用一个或两个示例进行解释。
of
返回带有指定的当前非空值的Optional。
此方法是用于创建Optional类的实例的工厂方法。 这里要注意的一点是,传递给创建实例的值必须为非null。 如果传递的值为null,则抛出NullPointerException
。
//Creating an instance of Optional using the factory method.
Optional<String> name = Optional.of("Sanaulla");
//This fails with a NullPointerException.
Optional<String> someNull = Optional.of(null);
ofNullable
返回描述指定值的Optional,如果非空,则返回空值。
与of
方法类似,唯一的区别是此方法还处理空值。 一个例子:
//This represents an instance of Optional containing no value
//i.e the value is 'null'
Optional empty = Optional.ofNullable(null);
isPresent
很简单的理解:
如果存在值,则返回true,否则返回false。
就像是:
//isPresent method is used to check if there is any
//value embedded within the Optional instance.
if (name.isPresent()) {//Invoking get method returns the value present//within the Optaional instance.System.out.println(name.get());//prints Sanaulla
}
get
如果此Optional中存在一个值,则返回该值,否则抛出NoSuchElementException。
此方法用于检索Optional实例中存在的值。 我们在上面看到了一个这样的例子。 让我们看一个抛出NoSuchElementException
的例子:
//The below code prints: No value present
try {//Invoking get method on an empty Optaional instance //throws NoSuchElementException.System.out.println(empty.get());
} catch (NoSuchElementException ex) {System.out.println(ex.getMessage());
}
ifPresent
如果存在值,请使用该值调用指定的使用者,否则不执行任何操作。
要了解此方法,您必须了解Consumer类 。 简而言之,Consumer是一个具有单个抽象方法的类,用于消费一些值并对其执行一些操作而不返回任何值。 在Java 8中,可以将lambda表达式传递给期望使用Consumer接口的方法。
如果Optional实例中存在该值,则上述方法接受代码/ lambda表达式块以执行某些操作。 就像是:
//ifPresent method takes a lambda expression as a parameter.
//The lambda expression can then consume the value if it is present
//and perform some operation with it.
name.ifPresent((value) -> {System.out.println("The length of the value is: " + value.length());
});
orElse
返回值(如果存在),否则返回其他。
此方法要么返回Optional实例中存在的值,否则返回返回作为参数传递给orElse
方法的值。 让我们看一个例子:
//orElse method either returns the value present in the Optional instance
//or returns the message passed to the method in case the value is null.
//prints: There is no value present!
System.out.println(empty.orElse("There is no value present!"));
//prints: Sanaulla
System.out.println(name.orElse("There is some value!"));
orElseGet
该方法类似于上述方法。 区别在于如何获取默认值。 在orElse
方法中,我们传递固定的字符串作为默认值,但在orElseGet
方法中,我们传递Supplier接口的实现,该接口具有用于生成默认值的方法。 让我们看一个例子:
//orElseGet is similar to orElse with a difference that instead of passing
//a default value, we pass in a lambda expression which generates the default
//value for us.
//prints: Default Value
System.out.println(empty.orElseGet(() -> "Default Value"));
//prints: Sanaulla
System.out.println(name.orElseGet(() -> "Default Value"));
orElseThrow
返回包含的值(如果存在),否则抛出异常,由提供的供应商创建。
就像在方法orElseGet
我们通过一个供应商的接口 ,但在orElseThrow
方法我们通过lambda表达式/方法引用抛出一个异常时,未找到该值。 一个例子:
try {//orElseThrow similar to orElse method, instead of returning a default//value, this method throws an exception which is generated from //the lambda expression/method reference passed as a param to the method.empty.orElseThrow(ValueAbsentException::new);
} catch (Throwable ex) {//prints: No value present in the Optional instanceSystem.out.println(ex.getMessage());
}
而且ValueAbsentException的定义是:
class ValueAbsentException extends Throwable {public ValueAbsentException() {super();}public ValueAbsentException(String msg) {super(msg);}@Overridepublic String getMessage() {return "No value present in the Optional instance";}
}
map
从有关地图方法的文档中:
如果存在值,则将提供的映射函数应用于该值,如果结果为非null,则返回描述结果的Optional。 否则,返回一个空的Optional。
此方法用于对Optional实例中存在的值应用一组操作。 这组操作以表示函数接口实现的lambda表达式的形式传递。 如果您不熟悉Function接口,那么请花一些时间在这里阅读我以前关于同一主题的博客文章。 让我们看一下map
方法的示例:
//map method modifies the value present within the Optional instance
//by applying the lambda expression passed as a parameter.
//The return value of the lambda expression is then wrapped into another
//Optional instance.
Optional<String> upperName = name.map((value) -> value.toUpperCase());
System.out.println(upperName.orElse("No value found"));
flatMap
从flatMap
方法的文档中:
如果存在一个值,则对其应用所提供的带有可选参数的映射函数,返回该结果,否则返回一个空的Optional。 此方法与map(Function)相似,但是提供的映射器是其结果已经是Optional的映射器,如果调用它,则flatMap不会使用附加的Optional对其进行包装。
此方法与map
方法非常相似,不同之处在于传递给它的映射函数的返回类型。 在map
方法的情况下,映射函数的返回值可以是任何类型T
,而在flatMap
方法的情况下,映射函数的返回值只能是Optional类型
让我们看一下上面的示例,其中将map
方法重写为flatMap
方法:
//flatMap is exactly similar to map function, the differece being in the
//return type of the lambda expression passed to the method.
//In the map method, the return type of the lambda expression can be anything
//but the value is wrapped within an instance of Optional class before it
//is returned from the map method, but in the flatMap method the return
//type of lambda expression's is always an instance of Optional.
upperName = name.flatMap((value) -> Optional.of(value.toUpperCase()));
System.out.println(upperName.orElse("No value found"));//prints SANAULLA
filter
通过将要应用于值的条件传递给filter
方法,该方法用于将值限制在Optional
实例内。 该文件说:
如果存在一个值,并且该值与给定谓词匹配,则返回描述该值的Optional,否则返回一个空的Optional。
到现在为止,您必须已经知道如何将一些代码块传递给该方法。 是的,它是lambda表达式。 对于这种方法,我们必须传递一个lambda表达式,该表达式将是Predicate接口的实现。 如果您不熟悉谓词界面,请花一些时间阅读这篇文章。
现在让我们看一下filter
方法的不同用法,即满足条件和不满足条件的两个示例。
//filter method is used to check if the given optional value satifies
//some condtion. If it satifies the condition then the same Optional instance
//is returned, otherwise an empty Optional instance is returned.
Optional<String> longName = name.filter((value) -> value.length() > 6);
System.out.println(longName.orElse("The name is less than 6 characters"));//prints Sanaulla//Another example where the value fails the condition passed to the
//filter method.
Optional<String> anotherName = Optional.of("Sana");
Optional<String> shortName = anotherName.filter((value) -> value.length() > 6);
//prints: The name is less than 6 characters
System.out.println(shortName.orElse("The name is less than 6 characters"));
借此,我向您介绍了Optional
类中存在的各种方法。 让我将以上所有示例汇总为一个示例,如下所示:
public class OptionalDemo {public static void main(String[] args) {//Creating an instance of Optional//This value can also be returned from some method. Optional<String> name = Optional.of("Sanaulla");//This represents an instance of Optional containing no value//i.e the value is 'null'Optional empty = Optional.ofNullable(null);//isPresent method is used to check if there is any //value embedded within the Optional instance.if (name.isPresent()) {//Invoking get method returns the value present//within the Optaional instance.System.out.println(name.get());}try {//Invoking get method on an empty Optaional instance //throws NoSuchElementException.System.out.println(empty.get());} catch (NoSuchElementException ex) {System.out.println(ex.getMessage());}//ifPresent method takes a lambda expression as a parameter.//The lambda expression can then consume the value if it is present//and perform some operation with it.name.ifPresent((value) -> {System.out.println("The length of the value is: " + value.length());});//orElse method either returns the value present in the Optional instance//or returns the message passed to the method in case the value is null.System.out.println(empty.orElse("There is no value present!"));System.out.println(name.orElse("There is some value!"));//orElseGet is similar to orElse with a difference that instead of passing //a default value, we pass in a lambda expression which generates the default //value for us.System.out.println(empty.orElseGet(() -> "Default Value"));System.out.println(name.orElseGet(() -> "Default Value"));try {//orElseThrow similar to orElse method, instead of returning a default//value, this method throws an exception which is genereated from //the lambda expression/method reference passed as a param to the method.empty.orElseThrow(ValueAbsentException::new);} catch (Throwable ex) {System.out.println(ex.getMessage());}//map method modifies the value present within the Optional instance//by applying the lambda expression passed as a parameter. //The return value of the lambda expression is then wrapped into another//Optional instance.Optional<String> upperName = name.map((value) -> value.toUpperCase());System.out.println(upperName.orElse("No value found"));//flatMap is exactly similar to map function, the differece being in the//return type of the lambda expression passed to the method.//In the map method, the return type of the lambda expression can be anything//but the value is wrapped within an instance of Optional class before it //is returned from the map method, but in the flatMap method the return //type of lambda expression's is always an instance of Optional.upperName = name.flatMap((value) -> Optional.of(value.toUpperCase()));System.out.println(upperName.orElse("No value found"));//filter method is used to check if the given optional value satifies//some condtion. If it satifies the condition then the same Optional instance//is returned, otherwise an empty Optional instance is returned.Optional<String> longName = name.filter((value) -> value.length() > 6);System.out.println(longName.orElse("The name is less than 6 characters"));//Another example where the value fails the condition passed to the //filter method.Optional<String> anotherName = Optional.of("Sana");Optional<String> shortName = anotherName.filter((value) -> value.length() > 6);System.out.println(shortName.orElse("The name is less than 6 characters"));}}
以及以上代码的输出:
Sanaulla
No value present
The length of the value is: 8
There is no value present!
Sanaulla
Default Value
Sanaulla
No value present in the Optional instance
SANAULLA
SANAULLA
Sanaulla
The name is less than 6 characters
翻译自: https://www.javacodegeeks.com/2013/09/deep-dive-into-optional-class-api-in-java-8.html
深入了解Java 8中的可选类API相关推荐
- java反射api研究_深入研究Java 8中的可选类API
java反射api研究 作为Java程序员,我们所有人都经历了以下情况:我们调用一个方法来获取某个值,然后代替直接对返回值调用某些方法,我们首先必须检查返回值是否不为null,然后在返回值. 这是像G ...
- Java 8 - Optional Class可选类
Java 8 - Optional Class可选类 1 Java 8-概述 2 Java 8 - Lambda表达式 3 Java 8 - 方法引用 4 Java8-Functional Inter ...
- java中main方法返回类型是6_[单选] Java application中的主类需包含main方法,main方法的返回类型是什么()。...
[单选] Java application中的主类需包含main方法,main方法的返回类型是什么(). 更多相关问题 关于超声在人体中传播的速度,叙述正确的有A.与人体组织的弹性有关B.与人体组织的 ...
- 在java程序中定义的类有两种成员_java试题 急需答案 谢谢!!!
三.填空(每小题2分,共10分)1.在Applet中,创建一个具有10行45列的多行文本区对象ta的语句为:2.创建一个标识有"关闭"字样的标签对象gb的语句为.3.方法是一种仅有 ...
- java程序设计专业介绍_简介Java编程中的Object类
这篇文章主要介绍了简介Java编程中的Object类,是Java入门学习中的基础知识,需要的朋友可以参考下 Object 类位于 java.lang 包中,是所有 Java 类的祖先,Java 中的每 ...
- java throwable判断,判断(2分) Java语言中的所有异常类都是java.lang.Throwable的子类。...
判断(2分) Java语言中的所有异常类都是java.lang.Throwable的子类. 更多相关问题 This artical is probably ________. 某企业2010年12月3 ...
- 一个java文件中多个类
一个.java文件中可以有很多类.不过注意以下几点: 1.public 权限的类只能有一个(也可以一个都没有,但最多只有1个) 2.这个.java文件的文件名必须是public类的类名(一般的情况下, ...
- java bufferedread_java中关于bufferedreader类中read方法
java中关于bufferedreader类中read方法 关注:58 答案:2 mip版 解决时间 2021-01-14 05:40 提问者孤城古巷 2021-01-13 06:28 如下.此时 ...
- JAVA语言中 文本框类的类名是_这是什么?
[简答题]设计一个Printer类继承Output和Product接口,实现数据的获取和输出 (25.0分) [多选题]一般Java程序的类主体由哪两部分组成( ). [多选题]如果子类中的( )与父 ...
最新文章
- centos ezhttp mysql_CentOS安装mysq
- Django rest_framework 实用技巧
- arcgis重心迁移分析_山东省植被覆盖度变化与气候因子相关性分析
- php调用md5.js,PHP和JS实现HTTP上安全地传输密码
- pta段错误是什么意思_用Python执行Django数据迁移时报!(1091错误及解决方法)...
- 百度AI学习-错误类型大全
- Android AsyncTask源码解析
- java中的scanner用法
- 3ds Max 中的导航控件SteeringWheels入门介绍
- python执行命令不阻塞_Python 命令行非阻塞输入
- 【知识图谱系列】基于Randomly Perturb的图谱预训练模型GraphCL
- mysql去掉两个最高分_Excel函数 去掉最高分和最低分取平均值?这个函数不可不知...
- hex2bin附源代码
- SecureCRT鼠标双击或拖成变成Ctrl+C的解决办法
- 计算两个时间之间的进度百分比
- Go:http request cancelled 服务端感知
- CoLA任务的数据增强方法
- instrument之Time Profiler总结
- 世界上最优秀的二十款防火墙
- FCC--Chunky Monkey(数组分组)和Slasher Flick(截断数组)
热门文章
- 用IDEA把SpringBoot项目打成jar发布项目 不要用 在上面有可以用的
- 怎么实现阴影效果呢?
- 使用junit进行单元测试_使用JUnit5对DynamoDB应用程序进行单元测试
- web.xml.jsf_面向初学者的JSF 2.0教程
- Java 9:Process API的增强
- 春天猫rtsy_春天重试,因为冬天来了
- java 开发:md5_Java社区调查结果:74%的开发人员希望减少详细程度
- java设计模式迭代器模式_迭代器设计模式示例
- redis nosql_Redis教程:NoSQL键值存储
- vue 脚手架测试环境_关于单元测试脚手架的几点思考