java8 new features six -Optional class

Brief introduction

Optional class is a container object may be a null. If the value exists isPresent () method returns true, call the get () method returns the object.

Optional is container: it can hold the value of type T, or just to save null. Optional offers many useful ways, so we do not explicitly be null detection.

Optional introduction of class a good solution to a null pointer exception.

 

Examples

public  class Java8Tester {
    public  static  void main (String args []) { 
   
      Java8Tester java8Tester = new new Java8Tester (); 
      Integer VALUE1 = null ; 
      Integer value2 = new new Integer (10 ); 
        
      // Optional.ofNullable - allows passing parameter is null 
      Optional < Integer> a = Optional.ofNullable (VALUE1); 
        
      // Optional.of - If the parameter passed is null, thrown a NullPointerException 
      Optional the <Integer> = B Optional.of (value2); 
      System.out.println (java8Tester.sum (A, B)); 
   } 
    
   publicSUM Integer (Optional The <Integer> A, Optional The <Integer> B) { 
    
      // Optional.isPresent - determination value exists 
        
      System.out.println ( "the first parameter value exists:" + a.isPresent ()); 
      the System .out.println ( "the second parameter values exist:" + b.isPresent ()); 
        
      // Optional.orElse - if the value is present, it is returned, otherwise the default value 
      Integer = VALUE1 a.orElse ( new new Integer (0 )); 
        
      // Optional.get - obtaining the value, the value needs to be present 
      Integer value2 = b.get ();
       return VALUE1 + value2; 
   } 
}

The implementation of the above script, output is:

The first parameter value exists: to false 
existence of the second parameter values: to true 
10

 

 

analysis

1, the above example only illustrates the use of option classes, and did not look like what's the use, before throwing NullPointerException, now or may throwing NullPointerException, NoSuchElementException etc., feeling not as good as previous == null so. Next we look at the method option class.

Class Methods

No. Method and Description
1 static <T> Optional<T> empty()

Optional return empty instance.

2 boolean equals(Object obj)

Determining whether another object is equal Optional.

3 Optional<T> filter(Predicate<? super <T> predicate)

If the value is present, and this value given the predicateA matches, it returns a value to describe Optional otherwise an empty Optional.

4 <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper)

If the value is present, the return value mapping method comprising Optional based, otherwise an empty Optional

5 T get()

If you include the value in this Optional, the return value, otherwise an exception is thrown: NoSuchElementException

6 int hashCode()

Returns the hash code value exists, returns 0 if the value does not exist.

7 void ifPresent(Consumer<? super T> consumer)

If the value exists then use that value to call consumer, or do nothing.

8 boolean isPresent()

It returns true if the value of the method exists, otherwise false.

9 <U>Optional<U> map(Function<? super T,? extends U> mapper)

If there is value, then calling the mapping function to get its return value. If the return value is not null, then create a map that contains Optional return value as the map method returns a value, otherwise return empty Optional.

10 static <T> Optional<T> of(T value)

Optional returns a non-null value specified.

11 static <T> Optional<T> ofNullable(T value)

If non-empty, return Optional The described specified value, otherwise empty Optional.

12 T orElse(T other)

If the value is present, the return value, otherwise other.

13 T orElseGet(Supplier<? extends T> other)

If the value is present, return values, or trigger other, and returns the result other call.

14 <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)

 

If the value is present, the value contained, otherwise an exception is thrown inherited by Supplier

15 String toString()

Optional returns a non-null string, used to debug

Note:  These methods from  java.lang.Object  inherited class come.

Guess you like

Origin www.cnblogs.com/wish5714/p/11345365.html