天天看點

Java Optional學習筆記

(1) 這個Optional.of方法還是不接收null作為輸入參數:

Java Optional學習筆記
而Optional.ofNullable可以接收null參數:
Java Optional學習筆記
isPresent判斷有無資料,這個沒啥可以說的:
Java Optional學習筆記
如果Optional容器包含的值不是null,執行action.
Java Optional學習筆記
orElse: 如果Optional對象存在,傳回原始對象,否則傳回orElse裡構造的新對象:
Java Optional學習筆記
上圖84行的getName其實是一個映射器,将Person對象映射成String.
Java Optional學習筆記
在debugger裡看到這個mapper實際上是個Lambda Function:
Java Optional學習筆記
結果是包裹類型為映射函數的第二個參數:
Java Optional學習筆記
ifPresent的參數是一個action:
Java Optional學習筆記
我的測試代碼:

package optionalTest;

import java.util.Optional;
import java.util.function.Function;

public class OptionalTest {

    class Person {
        private String mName;
        private Skill mSkill;
        public Person(String name) {
            mName = name;
        }
        
        public Skill getSkill(){
            return mSkill;
        }
        
        public Optional<Skill> getSkillOptional(){
            return Optional.ofNullable(mSkill);
        }
        public void setSkill(Skill skill){
            mSkill = skill;
        }
        public String getName() {
            return mName;
        }
        
        public void greet() {
            System.out.println("I am: " + mName);
        }
        
    }
    
    class Skill{
        private String mName;
        private int mLevel;
        public Skill(String name, int level) {
            mName = name;
            mLevel = level;
        }
        public void display(){
            System.out.println("Skill: " + mName + " level: " + mLevel);
        }
        
    }
    
    @SuppressWarnings("unused")
    public void scenario1(){
        Person person = null;
        try {
            Optional<Person> p1 = Optional.of(person);
        }
        catch ( Exception e) {
            System.out.println("Error: " + e.getClass().getName() + " - " + e.getLocalizedMessage());
        }
        
        Optional<Person> p2 = Optional.ofNullable(person);
        System.out.println(p2.isPresent());
        
        Person jerry = new Person("Jerry");
        Optional<Person> p3 = Optional.ofNullable(jerry);
        System.out.println(p3.isPresent());
        
        System.out.println("p2 test");
        p2.ifPresent((p) -> p.greet());
        System.out.println("p3 test");
        p3.ifPresent((p) -> p.greet());
        
        System.out.println("Example 3");
        // before Jave8
        Person oldImplementation = ( person  != null? person:new Person("Ji")); 
        Person newPerson = p2.orElse(new Person("Ji"));
        Person existPerson = p3.orElse(new Person("Mr Unknown"));
        newPerson.greet();
        existPerson.greet();
        
        if( jerry != null && jerry.getName().equals("Jerry")) {
            System.out.println("old style-> Jerry found: " + jerry.getName());
        }
        p3.filter( personHolder -> "Jerry".equals(personHolder.getName())).
            ifPresent((personH2) -> System.out.println("new style-> Jerry found: " + personH2.getName()));
        
        Function<Person,String> mapper = Person::getName;
        
        Optional<String> mapResult = p3.map(mapper);
        
        p3.map(Person::getName)
            .filter( Name -> "Jerry".equals(Name))
            .ifPresent( foundName -> System.out.println("new style 2-> Jerry found: " + foundName));
        
        /*jerry.setSkill("JavaScript");
        
        Optional<String> jerrySkill = p3.map(Person::getSkill);
        System.out.println(jerrySkill.isPresent());
        
        p3.map(Person::getSkill)
            .filter(Skill -> "JavaScript".equals(Skill))
            .ifPresent( Skill2 -> System.out.println("Jerry's skill is: " + Skill2 )  );*/
        
        Skill jsSkill = new Skill("JavaScript", 80);
        jerry.setSkill(jsSkill);
        
        Optional<Skill> temp = p3.map(Person::getSkill); 
        Optional<Skill> temp2 = p3.flatMap(Person::getSkillOptional);
        
        Skill s1 = temp.get();
        Skill s2 = temp2.get();
        
        s1.display();
        s2.display();

    }
    
    public static void main(String[] args) {
        OptionalTest test = new OptionalTest();
        test.scenario1();
    }

}