Optional Container class : Used to avoid null pointer exception as much as possible
Optional.of(T t) : Create a Optional example
Optional.empty() : Create an empty Optional example
Optional.ofNullable(T t): if t Not for null, establish Optional example , Otherwise, create an empty instance
isPresent() : Determine whether the value is included
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 s Value obtained
map(Function f): If there is value to deal with it , And return to the processed Optional, Otherwise return Optional.empty()

flatMap(Function mapper): And map similar , Required return value must be Optional

@Test public void test06(){ Optional<Student> op=Optional.ofNullable(null);
Optional<Integer> map = op.flatMap((e)->Optional.of(e.getAge()));
System.out.println(map);//Optional.empty Optional<Student>
op1=Optional.ofNullable(null); Optional<Integer> map1 =
System.out.println(map1.orElse(12));//12 } @Test public void test05(){
Optional<Student> op=Optional.ofNullable(null); Optional<Integer> map =
op.map((e)->e.getAge()); System.out.println(map);//Optional.empty
Optional<Student> op1=Optional.ofNullable(new Student(" Lili ")); Optional<String>
map1 = op1.map((e)->e.getName()); System.out.println(map1);//Optional[ Lili ] }
@Test public void test04(){ Optional< Student> op=Optional.ofNullable(new
Student(" Wang Li ")); Student orElseGet = op.orElseGet(()->{ Student s=new
Student(" Lily ",12,"ss"); return s; }); System.out.println(orElseGet);//Student
[getName()= Wang Li , getAge()=0, getSex()=null] Optional< Student>
op2=Optional.ofNullable(null); Student orElseGet2 = op2.orElseGet(()->{ Student
s=new Student(" Lily ",12,"ss"); return s; });
System.out.println(orElseGet2);//Student [getName()= Lily , getAge()=12,
getSex()=ss] } @Test public void test03(){ Student s=null; Optional<Student>
op2 = Optional.ofNullable(s); System.out.println("op2==="+op2.isPresent());
if(!op2.isPresent()){ System.out.println(op2.orElse(new Student())); } /** *
Student [getName()=null, getAge()=0, getSex()=null] */ } @Test public void
test02(){ Student s=null; Optional<Student> op1 = Optional.ofNullable(s);
//System.out.println(op1.get());//null System.out.println(op1.orElse(new
Student())); } @Test public void test01(){ Optional<Student> of =
Optional.of(new Student()); System.out.println("op"+of.get()); }    summary
: use Optional Can better avoid the occurrence of null pointer , Although we have to write code to judge , But no doubt this container class can make us write less if,else, Enhanced code readability ( Tell the truth if I don't like it ), and Lambda Can also be used together , It also simplifies the code when obtaining complex custom objects as default .

©2019-2020 Toolsou All rights reserved,
Bitcoin in ten years ,VDS Opportunity or fraud Don't annoy the panda with any cat !「 Kung Fu Panda 」20 It's the year of man 4 blood sort ( one ) bubble sort Random forest R Language implementation java Realize the function of grabbing red packets Python Basic knowledge and notes python To solve the problem of dictionary writing list in CSS architecture design Vue Common features ( one )2021 year 2 Monthly programmer salary statistics , average 15144 element