优雅的编码,使用Optional代替if-else
Optional是JAVA8引入的类,它其实是一个包装类,可以对所有对象进行包装, 包括null,这个特性使得我们编码可以优雅的解决空指针异常。
先编写一些测试类
class Student {
private ClassRoom classRoom; public ClassRoom getClassRoom() {
return classRoom;
} public void setClassRoom(ClassRoom classRoom) {
this.classRoom = classRoom;
}
} class ClassRoom {
private Seat seat; public Seat getSeat() {
return seat;
} public void setSeat(Seat seat) {
this.seat = seat;
}
} class Seat {
private Integer row; private Integer column; public Integer getRow() {
return row;
} public void setRow(Integer row) {
this.row = row;
} public Integer getColumn() {
return column;
} public void setColumn(Integer column) {
this.column = column;
}
}
先来看看以前传统我们编码怎么避免空指针异常的
Student student = new Student();
if (student != null) {
ClassRoom classRoom = student.getClassRoom();
if (classRoom != null) {
Seat seat = classRoom.getSeat();
if (seat != null) {
Integer row = seat.getRow();
System.out.println(row);
}
}
}
如果使用Optional的代码
Student student = new Student();
Integer row = Optional.ofNullable(student)
.map(Student::getClassRoom)
.map(ClassRoom::getSeat)
.map(Seat::getRow)
.orElse(null);
System.out.println(row);
重点在于orElse方法
public T orElse(T other) {
return value != null ? value : other;
}
方法里面做了非空判断,我们就可以传入一个如果对象为空时候的默认值;
可以看出Optional的方法许多都返回Optional对象,所以它支持链式调用;
而且大多方法入参都是Supplier 函数式接口,因此支持java8的JAVA Lambda表达式。
优雅的编码,使用Optional代替if-else的更多相关文章
- 学习Spring Boot:(十五)使用Lombok来优雅的编码
前言 Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO).它通过注解实现这一目的. 正文 添加依赖 在 pom.xml ...
- 使用Lombok来优雅的编码
介绍在项目中使用Lombok可以减少很多重复代码的书写.比如说getter/setter/toString等方法的编写. IDEA中的安装打开IDEA的Setting –> 选择Plugins选 ...
- 【代码工具】Lombok来优雅的编码
前言 Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO).它通过注解实现这一目的. 正文 添加依赖 在 pom.xml ...
- 是时候优雅的和NullPointException说再见了
是时候优雅的和NullPointException说再见了 ️️️️️️️️️️️️️️️️ 最近在参加原创投稿比赛,本篇文章如果对你有帮助的话,欢迎帮忙点击助力下吧 NullPointExcepti ...
- Protocol Buffers编码详解,例子,图解
Protocol Buffers编码详解,例子,图解 本文不是让你掌握protobuf的使用,而是以超级细致的例子的方式分析protobuf的编码设计.通过此文你可以了解protobuf的数据压缩能力 ...
- 如何在Angular优雅编写HTTP请求
原文:https://segmentfault.com/a/1190000010570799 ----------------------------------------------------- ...
- 为什么要使用Optional
为什么使用Java Optional Why use Optional? NullPointerException 有个很有名的说法: Null Pointer References: The Bil ...
- 夯实Java基础(二十四)——Java8新特征之Optional类
1.概述 对于Java程序员来说,到目前为止出现次数最多的应该是NullpointException,它是导致Java应用程序失败的最常见原因.之前处理空指针我们必须先通过条件先去判断,然后再确认是否 ...
- springboot 自定义属性
前言 spring boot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,我们如何添加呢 1.添加自定义属性 在src/main/resources/a ...
随机推荐
- Android程序员提加薪被拒,刷2000题跳槽涨薪50%!
为什么想跳槽? 简单说一下当时的状况,我在这家公司做了两年多,这两年多完成了一个大项目,作为开发的核心主力,开发压力很大,特别是项目上线前的几个月是非常辛苦,几乎每晚都要加班到12点以后,周末最多只有 ...
- asp .net core中swagger的简单使用
相信swagger大家不太陌生,简单来说就是把web api接口以ui的形式呈现到页面上,供方便调用和展示.这边文章就带大家初步简单使用swagger. (1)首先需要安装包:Swashbuckle. ...
- Longhorn 云原生分布式块存储解决方案设计架构和概念
内容来源于官方 Longhorn 1.1.2 英文技术手册. 系列 Longhorn 是什么? 目录 1. 设计 1.1. Longhorn Manager 和 Longhorn Engine 1.2 ...
- [数据结构]ODT(珂朵莉树)实现及其应用,带图
[数据结构]ODT(珂朵莉树)实现及其应用,带图 本文只发布于博客园,其他地方若出现本文均是盗版 算法引入 需要一种这样的数据结构,需要支持区间的修改,区间不同值的分别操作. 一般的,我们会想到用线段 ...
- 带你读AI论文丨用于目标检测的高斯检测框与ProbIoU
摘要:本文解读了<Gaussian Bounding Boxes and Probabilistic Intersection-over-Union for Object Detection&g ...
- 依赖注入@Autowired@Primary@Quelifier使用
@Autowired 注入声明的SpringBean对象,根据一定的规则首先按照注入的类型去查找,如果没有找到安装注入的名称去匹配你要注入的属性名称,如果都没有找到启动项目时抛出异常,@Autowir ...
- java关键字native、static、final详解
native: native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中.Java语言本身不能对操作系统底层进行访问和操作,但是可 ...
- 【java虚拟机】jvm内存模型
作者:pengjunlee原文链接:https://blog.csdn.net/pengjunlee/article/details/71909239 目录 一.运行时数据区域 1.程序计数器 2.J ...
- IDEA 创建Maven Web工程
一.Maven环境搭建 二.Maven常用命令 mvn clean 清除生成的target文件 mvn install 生成target文件 mvn clean install 相当于先删除targe ...
- win命令
netstat -nao | findstr "8888"taskkill /pid 15064 /f清理端口被占用win+r进入cmdcmd窗口中输入notepad进入记事本sh ...