java反射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接口的实现。 如果您不熟悉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

java反射api研究

java反射api研究_深入研究Java 8中的可选类API相关推荐

  1. 深入了解Java 8中的可选类API

    作为Java程序员,我们所有人都经历了以下情况:我们调用一个方法来获取某个值,然后代替直接对返回值调用某些方法,我们首先必须检查返回值不为null,然后在返回值. 这是像Guava这样的外部API试图 ...

  2. java 反射 动态编译_动态编译java源代码和反射调用问题

    我从教程中得到了以下代码: package com.tom.labs; import java.io.IOException; import java.lang.reflect.Method; imp ...

  3. java反射获取泛型_如何通过Java反射获取泛型类型信息

    前言 关于Java泛型,很多人都有一个误解,认为Java代码在编译时会擦除泛型的类型,从而在运行时导致没法访问其类型,这其实并不完全正确,因为有一部分泛型信息是可以在运行时动态获取的,这部分信息基本能 ...

  4. java反射和注解开发(备java基础,javaee框架原理)-任亮-专题视频课程

    java反射和注解开发(备java基础,javaee框架原理)-5358人已学习 课程介绍         Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明. ...

  5. java 获取 反射 方法 名_乐字节Java反射之一:反射概念与获取反射源头Class

    一.Java反射机制概念 "程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言",如Python, Ruby是动态语言:显然C++,Java,C#不是动态语言,但是JAV ...

  6. java反射头文件_编程基础知识——C++能不能支持Java和ObjC的反射?

    C++能不能支持Java和ObjC的反射? 要回答这个问题.首先我们要清楚什么是反射.什么是反射? 教科书的解释我就不说了,(^o^)事实上我也记不得.实际开发应用的反射就是在没有某个类型的头文件或者 ...

  7. java 反射机制 视频_【视频笔记】Java反射机制笔记

    Java 语言的反射机制 在Java运行时环境中,对于任意一个类,可以知道这个类有哪些属性和方法.对于任意一个对象,可以调用它的任意一个方法. 这种动态获取类的信息以及动态调用对象的方法的功能来自于J ...

  8. java 反射的效率_如何提高使用Java反射的效率?

    前言 在我们平时的工作或者面试中,都会经常遇到"反射"这个知识点,通过"反射"我们可以动态的获取到对象的信息以及灵活的调用对象方法等,但是在使用的同时又伴随着另 ...

  9. java反射invoke空指针_【Java】Java 反射 object is not an instance of declaring cla

    [Java]Java 反射 object is not an instance of declaring cla [Java]Java 反射 object is not an instance of ...

最新文章

  1. iOS RunLoop简介
  2. 50岁,他希望自己还可以写代码
  3. integral函数
  4. Stanford UFLDL教程 卷积特征提取
  5. vfp 右键发送邮件_邮件批量发送的方法教程
  6. GDI+ 使用指南(basic guiding of GDI plus )
  7. 从VC++到GCC移植:谈两者的语法差异
  8. zabbix api python使用
  9. Crackeme021
  10. Setup Factory 打包工具部分功能代码解
  11. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.4——更新新版本的Gradle...
  12. java怎么设置窗体title_自定义Java窗口标题栏菜单
  13. C语言 求两个数最小公倍数和最大公因数
  14. 微型计算机系统核心hhg,计算机应用基础测试题1
  15. cvte软件测试笔试题,CVTE前端笔试20190302
  16. 互联网领袖高峰对话:大佬们之间的对掐
  17. 天天自习软件测试计划
  18. 垃圾收集器G1与ZGC
  19. luooj1559最强阵容加强版
  20. SpringDataJPA+Hibernate框架源码剖析(六)@PersistenceContext和@Autowired注入EntityManager的区别

热门文章

  1. P2604 ZJOI2010 网络扩容,费用流裸题
  2. 动态规划训练19、最短路 [Help Jimmy POJ - 1661 ]
  3. Nacos(四)之安装
  4. TCP为什么是三次握手和四次挥手
  5. JavaFX鼠标拖拽事件
  6. 给你一份超详细 Spring Boot 知识清单
  7. jQuery动画与事件概念以及语法
  8. 对象拷贝的工具类DeepBeanUtils
  9. JDBC登录功能实现
  10. 程序员成长之路 java面试指导(作者说的极好要看) 静下心看