优雅的编码,使用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 ...
随机推荐
- Docker for windows安装与使用
1.安装Docker for windows之前,需要将系统的hyper-v功能打开 2.下载Docker for windows进行安装 访问url:https://docs.docker.com/ ...
- Java多线程间的数据共享
下面的程序演示了一个对象被两个线程访问的方法,"monitor.gotMessage();"这一句虽然是monitor对象的方法,但却是运行在"MyObject" ...
- contos7 安装weblogic10.3 _wls1036_generic.jar
环境:CentOS7+jdk1.8 weblogic下载地址1: http://www.oracle.com/technetwork/cn/middleware/weblogic/downloads/ ...
- Liunx搭建Rlogin服务
实验环境为 centos7 第一步:安装服务:yum -y install rsh rsh-server xinetd 第二步:启动服务: systemctl restart rsh.sockets ...
- SQL 练习18
按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 SELECT t.cid,t.sid,t.score ,COUNT(t1.score)+1 as 排名 from sc as t LE ...
- [TcaplusDB知识库]数据库支撑底盘引擎计算层介绍
在上次的TcaplusDB知识库中,TcaplusDB君为大家讲解了TcaplusDB所用的基于HASH表的Key-value存储引擎TXHDB.存储引擎作为数据库的支撑底盘,其重要性无可置疑,而在本 ...
- GitLabRunner命令
启动命令 gitlab-runner --debug <command> #调试模式排查错误特别有用. gitlab-runner <command> --help #获取帮助 ...
- 1、Task的优势
1.Task的优势 ThreadPool相比Thread来说具备了很多优势,但是ThreadPool却又存在一些使用上的不方便.比如: ◆ ThreadPool不支持线程的取消.完成.失败通知等交互性 ...
- 【java文件处理】java项目路径下的文件下载处理
1. controller类: package com.neo.controller; import javax.servlet.http.HttpServletResponse; import or ...
- 【springcloud】springcloud与springboot的版本对应关系
官方网址:https://start.spring.io/actuator/info 更新时间:2019-12-01 spring-cloud: "Finchley.M2": &q ...