Java8 introduced the Optional class to avoid null pointer exceptions, and pay attention to reduce them as much as possible.
Common methods of Optional:
Constructor:
Optional.of(T t) : Create an Optional instance
Optional.empty() : Create an empty Optional instance
Optional.ofNullable(T t) : If t is not null, create an Optional instance, otherwise create an empty instance
isPresent () : Determine if it contains a value
Common method:
orElse(T t) : if the calling object contains a value, return the value, otherwise return t
orElseGet(Supplier s) : if the calling object contains a value, return the value, otherwise return the value obtained by s
map(Function f) : if there is a value Process it and return the processed Optional, otherwise return Optional.empty()
flatMap(Function mapper) : Similar to map, the return value must be Optional
Example:
package com.jv.java8.optional;
public class Cust {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Cust other = (Cust) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Cust [name=" + name + "]";
}
public Cust() {
super();
// TODO Auto-generated constructor stub
}
public Cust(String name) {
super();
this.name = name;
}
}
package com.jv.java8.optional;
public class Account {
private Cust cust;
private String name;
private Integer gendor;
public Cust getCust() {
return cust;
}
public void setCust(Cust cust) {
this.cust = cust;
}
@Override
public String toString() {
return "Account [cust=" + cust + ", name=" + name + ", gendor=" + gendor + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cust == null) ? 0 : cust.hashCode());
result = prime * result + ((gendor == null) ? 0 : gendor.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Account other = (Account) obj;
if (cust == null) {
if (other.cust != null)
return false;
} else if (!cust.equals(other.cust))
return false;
if (gendor == null) {
if (other.gendor != null)
return false;
} else if (!gendor.equals(other.gendor))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getGendor() {
return gendor;
}
public void setGendor(Integer gendor) {
this.gendor = gendor;
}
public Account() {
super();
// TODO Auto-generated constructor stub
}
public Account(Cust cust, String name, Integer gendor) {
super();
this.cust = cust;
this.name = name;
this.gendor = gendor;
}
}
package com.jv.java8.optional;
import java.util.Optional;
import org.junit.Test;
public class TestOptional {
@Test
public void test1() {
//制造一个空指针异常
Account acct = new Account();
System.out.println(acct.getCust().getName());//如愿以偿报了空指针异常,因为默认构造函数得到的ACCOUNT对象的CUST是空的
}
@Test
public void test2() {
NewAccount nAcct = new NewAccount();
System.out.println(nAcct.getCust().get());
/**
* 走到这里可能有老铁要问了,你还不是报异常了啊,虽然不是空指针异常,但是有了Optional,你可以很容易想到这种异常是出现在哪里,精彩的请继续往后看
*/
}
@Test
public void test3() {
NewAccount nAcct = new NewAccount();
System.out.println(nAcct.getCust().orElse(new Cust("乔布斯")));//很简单明了实现了我们之前好多行才能解决的问题吧
NewAccount acct = new NewAccount(Optional.of(new Cust("乔布斯")),"比尔盖茨",1);
System.out.println(acct.getCust().map(x->x.getName()).get());
}
}
For me, who is used to writing if else, is it really cost-effective to use Optional in large-scale systems, especially when there are many object fields and it is easy to be empty? ? ?