Home Java javaTutorial 解析Java 8 Optional类实例教程

解析Java 8 Optional类实例教程

May 24, 2017 am 11:47 AM

        身为一名Java程序员,大家可能都有这样的经历:调用一个方法得到了返回值却不能直接将返回值作为参数去调用别的方法。我们首先要判断这个返回值是否为null,只有在非空的前提下才能将其作为其他方法的参数。这正是一些类似Guava的外部API试图解决的问题。一些JVM编程语言比如Scala、Ceylon等已经将对在核心API中解决了这个问题。在我的前一篇文章中,介绍了Scala是如何解决了这个问题。

新版本的Java,比如Java 8引入了一个新的Optional类。Optional类的Javadoc描述如下:

这是一个可以为null的容器对象。如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象。
Copy after login
本文会逐个探讨Optional类包含的方法,并通过一两个示例展示如何使用。

of

为非null的值创建一个Optional。
Copy after login
of方法通过工厂方法创建Optional类。需要注意的是,创建对象时传入的参数不能为null。如果传入参数为null,则抛出NullPointerException 。

1

2

3

4


//调用工厂方法创建Optional实例

Optional<<a href="http://www.php.cn/wiki/57.html" target="_blank">String</a>> name = Optional.of("Sanaulla");

//传入参数为null,抛出NullPointerException.

Optional<String> someNull = Optional.of(null);


ofNullable

为指定的值创建一个Optional,如果指定的值为null,则返回一个空的Optional。

ofNullable与of方法相似,唯一的区别是可以接受参数为null的情况。示例如下:

1

2

3


//下面创建了一个不包含任何值的Optional实例

//例如,值为&#39;null&#39;

Optional empty = Optional.ofNullable(null);


isPresent

非常容易理解

如果值存在返回true,否则返回false。

类似下面的代码:

1

2

3

4

5


//isPresent方法用来检查Optional实例中是否包含值

if(name.isPresent()) {

//在Optional实例内调用get()返回已存在的值

System.out.<a href="http://www.php.cn/wiki/1362.html" target="_blank">print</a>ln(name.get());//输出Sanaulla

}


get

如果Optional有值则将其返回,否则抛出NoSuchElementException。

上面的示例中,get方法用来得到Optional实例中的值。下面我们看一个抛出NoSuchElementException的例子:

1

2

3

4

5

6

7


//执行下面的代码会输出:No value present

try{

//在空的Optional实例上调用get(),抛出NoSuchElementException

System.out.println(empty.get());

}catch(NoSuchElementException ex) {

System.out.println(ex.getMessage());

}


ifPresent

如果Optional实例有值则为其调用consumer,否则不做处理

要理解ifPresent方法,首先需要了解Consumer类。简答地说,Consumer类包含一个抽象方法。该抽象方法对传入的值进行处理,但没有返回值。Java8支持不用接口直接通过lambda表达式传入参数。

如果Optional实例有值,调用ifPresent()可以接受接口段或lambda表达式。类似下面的代码:

1

2

3

4

5


//ifPresent方法接受lambda表达式作为参数。

//lambda表达式对Optional的值调用consumer进行处理。

name.ifPresent((value) -> {

System.out.println("The length of the value is: " + value.length());

});


orElse

如果有值则将其返回,否则返回指定的其它值。

如果Optional实例有值则将其返回,否则返回orElse方法传入的参数。示例如下:

1

2

3

4

5

6


//如果值不为null,orElse方法返回Optional实例的值。

//如果为null,返回传入的消息。

//输出:There is no value present!

System.out.println(empty.orElse("There is no value present!"));

//输出:Sanaulla

System.out.println(name.orElse("There is some value!"));

orElseGet

orElseGet与orElse方法类似,区别在于得到的默认值。orElse方法将传入的字符串作为默认值,orElseGet方法可以接受Supplier接口的实现用来生成默认值。示例如下:

1

2

3

4

5

6


//orElseGet与orElse方法类似,区别在于orElse传入的是默认值,

//orElseGet可以接受一个lambda表达式生成默认值。

//输出:Default Value

System.out.println(empty.orElseGet(() -> "Default Value"));

//输出:Sanaulla

System.out.println(name.orElseGet(() -> "Default Value"));


orElseThrow

如果有值则将其返回,否则抛出supplier接口创建的异常。

在orElseGet方法中,我们传入一个Supplier接口。然而,在orElseThrow中我们可以传入一个lambda表达式或方法,如果值不存在来抛出异常。示例如下:

1

2

3

4

5

6

7

8

9


try{

//orElseThrow与orElse方法类似。与返回默认值不同,

//orElseThrow会抛出lambda表达式或方法生成的异常

empty.orElseThrow(ValueAbsentException::new);

}catch(Throwable ex) {

//输出: No value present in the Optional instance

System.out.println(ex.getMessage());

}


ValueAbsentException定义如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15


<a href="http://www.php.cn/wiki/164.html" target="_blank">class</a>ValueAbsentException <a href="http://www.php.cn/wiki/166.html" target="_blank">extends</a>Throwable {

publicValueAbsentException() {

<a href="http://www.php.cn/code/8202.html" target="_blank">super</a>();

}

publicValueAbsentException(String msg) {

super(msg);

}

@Override

publicString getMessage() {

<a href="http://www.php.cn/wiki/135.html" target="_blank">return</a>"No value present in the Optional instance";

}

}


map

map方法文档说明如下:

如果有值,则对其执行调用mapping函数得到返回值。如果返回值不为null,则创建包含mapping返回值的Optional作为map方法返回值,否则返回空Optional。

map方法用来对Optional实例的值执行一系列操作。通过一组实现了Function接口的lambda表达式传入操作。如果你不熟悉Function接口,可以参考我的这篇博客。map方法示例如下:

1

2

3

4


//map方法执行传入的lambda表达式参数对Optional实例的值进行修改。

//为lambda表达式的返回值创建新的Optional实例作为map方法的返回值。

Optional<String> upperName = name.map((value) -> value.toUpperCase());

System.out.println(upperName.orElse("No value found"));


flatMap

如果有值,为其执行mapping函数返回Optional类型返回值,否则返回空Optional。flatMap与map(Funtion)方法类似,区别在于flatMap中的mapper返回值必须是Optional。调用结束时,flatMap不会对结果用Optional封装。

flatMap方法与map方法类似,区别在于mapping函数的返回值不同。map方法的mapping函数返回值可以是任何类型T,而flatMap方法的mapping函数必须是Optional。

参照map函数,使用flatMap重写的示例如下:

1

2

3

4

5


//flatMap与map(Function)非常类似,区别在于传入方法的lambda表达式的返回类型。

//map方法中的lambda表达式返回值可以是任意类型,在map函数返回之前会包装为Optional。

//但flatMap方法中的lambda表达式返回值必须是Optionl实例。

upperName = name.flatMap((value) -> Optional.of(value.toUpperCase()));

System.out.println(upperName.orElse("No value found"));//输出SANAULLA


filter

filter个方法通过传入限定条件对Optional实例的值进行过滤。文档描述如下:

如果有值并且满足断言条件返回包含该值的Optional,否则返回空Optional。

读到这里,可能你已经知道如何为filter方法传入一段代码。是的,这里可以传入一个lambda表达式。对于filter函数我们应该传入实现了Predicate接口的lambda表达式。如果你不熟悉Predicate接口,可以参考这篇文章。

现在我来看看filter的各种用法,下面的示例介绍了满足限定条件和不满足两种情况:

1

2

3

4

5

6

7

8

9

10


//filter方法检查给定的Option值是否满足某些条件。

//如果满足则返回同一个Option实例,否则返回空Optional。

Optional<String> longName = name.filter((value) -> value.length() > 6);

System.out.println(longName.orElse("The name is less than 6 characters"));//输出Sanaulla

//另一个例子是Optional值不满足filter指定的条件。

Optional<String> anotherName = Optional.of("Sana");

Optional<String> shortName = anotherName.filter((value) -> value.length() > 6);

//输出:name长度不足6字符

System.out.println(shortName.orElse("The name is less than 6 characters"));

以上,我们介绍了Optional类的各个方法。下面通过一个完整的示例对用法集中展示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69


publicclass OptionalDemo {

publicstatic void <a href="http://www.php.cn/wiki/646.html" target="_blank">main</a>(String[] args) {

//创建Optional实例,也可以通过方法返回值得到。

Optional<String> name = Optional.of("Sanaulla");

//创建没有值的Optional实例,例如值为&#39;null&#39;

Optional empty = Optional.ofNullable(null);

//isPresent方法用来检查Optional实例是否有值。

if(name.isPresent()) {

//调用get()返回Optional值。

System.out.println(name.get());

}

try{

//在Optional实例上调用get()抛出NoSuchElementException。

System.out.println(empty.get());

}catch(NoSuchElementException ex) {

System.out.println(ex.getMessage());

}

//ifPresent方法接受lambda表达式参数。

//如果Optional值不为空,lambda表达式会处理并在其上执行操作。

name.ifPresent((value) -> {

System.out.println("The length of the value is: " + value.length());

});

//如果有值orElse方法会返回Optional实例,否则返回传入的<a href="http://www.php.cn/php/php-tp-errormessage.html" target="_blank">错误信息</a>。

System.out.println(empty.orElse("There is no value present!"));

System.out.println(name.orElse("There is some value!"));

//orElseGet与orElse类似,区别在于传入的默认值。

//orElseGet接受lambda表达式生成默认值。

System.out.println(empty.orElseGet(() -> "Default Value"));

System.out.println(name.orElseGet(() -> "Default Value"));

try{

//orElseThrow与orElse方法类似,区别在于返回值。

//orElseThrow抛出由传入的lambda表达式/方法生成异常。

empty.orElseThrow(ValueAbsentException::new);

}catch(Throwable ex) {

System.out.println(ex.getMessage());

}

//map方法通过传入的lambda表达式修改Optonal实例默认值。

//lambda表达式返回值会包装为Optional实例。

Optional<String> upperName = name.map((value) -> value.toUpperCase());

System.out.println(upperName.orElse("No value found"));

//flatMap与map(Funtion)非常相似,区别在于lambda表达式的返回值。

//map方法的lambda表达式返回值可以是任何类型,但是返回值会包装成Optional实例。

//但是flatMap方法的lambda返回值总是Optional类型。

upperName = name.flatMap((value) -> Optional.of(value.toUpperCase()));

System.out.println(upperName.orElse("No value found"));

//filter方法检查Optiona值是否满足给定条件。

//如果满足返回Optional实例值,否则返回空Optional。

Optional<String> longName = name.filter((value) -> value.length() > 6);

System.out.println(longName.orElse("The name is less than 6 characters"));

//另一个示例,Optional值不满足给定条件。

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 inthe Optional instance
SANAULLA
SANAULLA
Sanaulla
The
 name is lessthan 6 characters
Copy after login

【相关推荐】

 1. Java 8新增的API--Optional 的使用实例

 2. 分享Java8中新引入的类Optional实例代码

The above is the detailed content of 解析Java 8 Optional类实例教程. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles