欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

使用java8中的Optional API解决nullPointException问题

程序员文章站 2022-05-16 09:48:37
今天和大家一起使用java8中的optional api解决nullpointexception, 先来看一段代码: public class car { private insur...

今天和大家一起使用java8中的optional api解决nullpointexception,

先来看一段代码:


 public class car {
  private insurance insurance;
  public insurance getinsurance() {
return insurance;
  }
 }


 public class insurance {
  private string name;
  public string getname() {
return name;
  }
 }


 public class person {
  private car car;
  public car getcar() {
return car;
  }
 }

 public string getcarinsurancename(person p){
  return p.getcar().getinsurance().getname();
 }

这个getcarinsurancename()方法一看就知道可能有nullpointexception

通常我们这样处理:

public string getcarinsurancename(person p){
  if(p!=null){
 car car = p.getcar();
 if(car!=null){
 insurane insurance = car.getinsurance();
 if(insurance!=null){
 return insurance.getname();
 }
 }
  }
  return "unknow";
}
public class person {
 private optional car;
 public optional getcar() {
  return car;
 }
}

public class car {
 private optional insurance;
 public optional getinsurance() {
  return insurance;
 }
}

public class insurance {
 private string name;
 public string getname() {
  return name;
 }
}

 public string getcarinsurancename(optional person) {
  return person.flatmap(person::getcar)
.flatmap(car::getinsurance)
.map(insurance::getname)
.orelse("unknown");
}

一脸懵逼有没有?好的大王,不管你有没有被征服,反正我是被臣妾是要开始

娓娓道来了。

上面的getcarinsurancename方法的几个关键点:

1:使用lambda表达式(不会的请恶补上篇文章)

2:使用java.util.optional对person类中的car类和car类中的insurance类包装

3:使用java.util.optional 中流式api处理数据(类似rxjava或者java.util.steam)

偷偷的把optional类实例方法截了图

使用java8中的Optional API解决nullPointException问题

optional构造函数

1:空值optional

optional optcar = optional.empty();

2:非空值optional,传空抛异常

optional optcar = optional.of(car);

3:可接受null的optional

optional optcar = optional.ofnullable(car);

使用map从optional中取值和转换值

optional optinsurance = optional.ofnullable(insurance);

optional name = optinsurance.map(insurance::getname);

使用flatmap转换optional

 

optional optcar= person.flatmap(person::getcar);

optional = optcar.flatmap(car::getinsurance)

map和flatmap区别:

optional> car = optperson.map(person::getcar);

optional car1 = optperson.flatmap(person::getcar);

ifpresent:如果值存在就会执行回调,否则什么也不干

optcar.ifpresent(car::getinsurance);

ispresent:值存在返回true,否则false

boolean = optcar.ispresent;

orelse:有值则返回,否则返回一个默认值

string name = optcar.map(insurance::getname)

.orelse("unknown");

andthen,再来个filter的例子

 @requiresapi(api = build.version_codes.n)
 public static void main(string[] args) {
  properties properties = new properties();
  properties.setproperty("a", "5");
  properties.setproperty("b", "-1");
  properties.setproperty("c", "true");
  system.out.println(readduration(properties,"a"));
  system.out.println(readduration(properties,"b"));
  system.out.println(readduration(properties,"c"));
  optionalint.of(1).
 }
 @requiresapi(api = build.version_codes.n)
 public static int readduration(properties properties, string name) {
  return optional.ofnullable(properties.getproperty(name))
 .flatmap(s -> parsestring(s))
 .filter(i -> i > 10)
 .orelse(0);
 }

 @requiresapi(api = build.version_codes.n)
 public static  optional parsestring(string s) {
  optional optionalint;
  try {
optionalint = optional.of(integer.parseint(s));
  } catch (numberformatexception e) {
optionalint = optional.empty();
  }
  return optionalint;
 }

好了,通过上面的这些例子可以帮助我们更优雅的处理nullpointexception,祝福各位撸的愉快