深入了解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 instance
  System.out.println(ex.getMessage());
}

而且ValueAbsentException的定义是:

class ValueAbsentException extends Throwable {

  public ValueAbsentException() {
    super();
  }

  public ValueAbsentException(String msg) {
    super(msg);
  }

  @Override
  public 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

参考:Experiences Unlimited博客上,我们的JCG合作伙伴 Mohamed Sanaulla深入研究了Java 8中的Optional类API

翻译自: https://www.javacodegeeks.com/2013/09/deep-dive-into-optional-class-api-in-java-8.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值