Optional common method of containers

1, the conventional method of containers

// . 1, Optional.of (T T): Create an instance can not pass Optional null 
Optional <the Employee> Optional.of OP1 = ( new new the Employee ()); 

// 2, Optional.empty (): Create an empty Optional examples of 
Optional <the Employee> OP2 = Optional.empty (); 

// . 3, Optional.ofNullable (Tt of the): If t is not created Optional examples nul1,, or create an empty instance 
Optional <the Employee> OP3 = Optional.ofNullable ( new new the Employee ());
 // . 4, isPresent (): comprising determining whether the value 
IF (op3.isPresent ()) { 
    System.out.println (op3.get ()); 
} 

// . 5, orElse (Tt of the): If the call object contains a value, the return value, otherwise T 
the Employee Employee = op3.orElse ( new new the Employee (. 11, "AAAA" )); 
System.out.println (Employee);

// . 6, orElseGet (Supplier s): If the calling object contains a value, the return value, otherwise the return value s obtained 
the Employee Employee2 op3.orElseGet = (() -> new new the Employee ()); 
System.out.println (Employee2 ); 

// . 7, Map (Function F): if the value of its processing, and returns the processed Optional, otherwise Optional.empty () 
Optional <the Employee> OP4 = Optional.ofNullable ( new new the Employee (. 1, "Zhang three " )); 
Optional the <Integer> = op4.map map (E -> e.getAge ()); 
System.err.println (as map.get ()); 

// . 8, flatMap (Function Mapper): and map Similarly, the return value must be required Optional The 
Optional The <the Employee> OP5 = Optional.ofNullable ( new new the Employee (. 1, "Joe Smith" )); 
Optional The <String> flatMap op5.flatMap = (E -> Optional.of(e.getName()));
System.err.println(flatMap.get());

 

Guess you like

Origin www.cnblogs.com/zhanh247/p/11869965.html