Java 8 - Optional Class

Optional类是对象的容器(Container),Optional对象用来表示值为空(null)的对象。Optional类提供了对象是否可以访问的方法,而不用手动检查对象是否为null,这类似于Guava的Optional。

类描述

下面是java.util.Optional类的声明

public final class Optional<T> extends Object
类提供的方法
No.Method & Description
1static <T> Optional<T> empty() - Returns an empty Optional instance.
2boolean equals(Object obj) - Indicates whether some other object is “equal to” this Optional.
3Optional<T> filter(Predicate<? super <T> predicate) - If a value is present and the value matches a given predicate, it returns an Optional describing the value, otherwise returns an empty Optional.
4<U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper) - If a value is present, it applies the provided Optional-bearing mapping function to it, returns that result, otherwise returns an empty Optional.
5T get() - If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
6int hashCode() - Returns the hash code value of the present value, if any, or 0 (zero) if no value is present.
7void ifPresent(Consumer<? super T> consumer) - If a value is present, it invokes the specified consumer with the value, otherwise does nothing.
8boolean isPresent() - Returns true if there is a value present, otherwise false.
9<U>Optional<U> map(Function<? super T,? extends U> mapper) - If a value is present, applies the provided mapping function to it, and if the result is non-null, returns an Optional describing the result.
10static <T> Optional<T> of(T value) - Returns an Optional with the specified present non-null value.
11static <T> Optional<T> ofNullable(T value) - Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
12T orElse(T other) - Returns the value if present, otherwise returns other.
13T orElseGet(Supplier<? extends T> other) - Returns the value if present, otherwise invokes other and returns the result of that invocation.
14<X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) - Returns the contained value, if present, otherwise throws an exception to be created by the provided supplier.
15String toString() - Returns a non-empty string representation of this Optional suitable for debugging.
示例
import java.util.Optional;

public class Java8Tester {

   public static void main(String args[]) {
      Java8Tester java8Tester = new Java8Tester();
      Integer value1 = null;
      Integer value2 = new Integer(10);
		
      //Optional.ofNullable - allows passed parameter to be null.
      Optional<Integer> a = Optional.ofNullable(value1);
		
      //Optional.of - throws NullPointerException if passed parameter is null
      Optional<Integer> b = Optional.of(value2);
      System.out.println(java8Tester.sum(a,b));
   }
	
   public Integer sum(Optional<Integer> a, Optional<Integer> b) {
      //Optional.isPresent - checks the value is present or not
		
      System.out.println("First parameter is present: " + a.isPresent());
      System.out.println("Second parameter is present: " + b.isPresent());
		
      //Optional.orElse - returns the value if present otherwise returns
      //the default value passed.
      Integer value1 = a.orElse(new Integer(0));
		
      //Optional.get - gets the value, value should be present
      Integer value2 = b.get();
      return value1 + value2;
   }
}

输出结果:

First parameter is present: false
Second parameter is present: true
10

原文来自:https://www.tutorialspoint.com/java8/java8_optional_class.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值