Optional common operations

1. Common operations

 @Test
    public void test1() {
        F f = new F();
        // of(非null对象)
        Optional<F> fOptional = Optional.of(f);
        System.out.println(fOptional);
        F = null ;
         // ofNullable (may be null objects) 
        Optional The <F.> fOptional1 = Optional.ofNullable (F);
        System.out.println(fOptional1);
        // orElse (T T), if the package Optional entity is not empty, the object returns Optional packaging, otherwise T 
        F. FOptional1.orElse F1 = ( new new F. ( "ADMIN" ));
        System.out.println(f1);

        // T GET (): if the object contains the value of the call, the return value, otherwise throw an exception
         // T orElse (T OTHER): If there is a value, the return value, otherwise return OTHER
         // ? T orElseGet (Supplier <Extend T> other): If there is a value returns that value, otherwise it returns OTHER
         // T orElseThrow (Supplier <Extend the X-> exceptionSupplier):? has a value returns that value, otherwise throw an exception 
    }

 

Guess you like

Origin www.cnblogs.com/z-qinfeng/p/11767911.html