了解网站建设,贵州建设厅网站,本溪市网站建设,wordpress三栏主题作为Java程序员#xff0c;我们所有人都经历了以下情况#xff1a;我们调用一个方法来获取某个值#xff0c;然后代替直接对返回值调用某些方法#xff0c;我们首先必须检查返回值不为null#xff0c;然后在返回值。 这是像Guava这样的外部API试图解决的难题 。 此外… 作为Java程序员我们所有人都经历了以下情况我们调用一个方法来获取某个值然后代替直接对返回值调用某些方法我们首先必须检查返回值不为null然后在返回值。 这是像Guava这样的外部API试图解决的难题 。 此外其他JVM语言例如ScalaCeylon等也将这些功能嵌入了核心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.
OptionalString name Optional.of(Sanaulla);
//This fails with a NullPointerException.
OptionalString 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.
OptionalString upperName name.map((value) - value.toUpperCase());
System.out.println(upperName.orElse(No value found)); flatMap 从flatMap方法的文档中 如果存在一个值则对其应用所提供的带有可选参数的映射函数返回该结果否则返回一个空的Optional。 此方法与mapFunction相似但是提供的映射器是其结果已经是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 expressions 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.
OptionalString 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.
OptionalString anotherName Optional.of(Sana);
OptionalString 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. OptionalString name Optional.of(Sanaulla);//This represents an instance of Optional containing no value//i.e the value is nullOptional 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.OptionalString 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 expressions 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.OptionalString 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.OptionalString anotherName Optional.of(Sana);OptionalString 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