📅  最后修改于: 2021-01-05 04:40:29             🧑  作者: Mango
可选的是用于包含非空对象的容器对象。可选对象用于表示缺少值的null。此类具有各种实用程序方法,以方便代码将值处理为“可用”或“不可用”,而不是检查空值。它是Java 8中引入的,类似于Guava中的Optional。
以下是java.util.Optional
public final class Optional extends Object
Sr.No. | Method & Description |
---|---|
1 |
static Returns an empty Optional instance. |
2 |
boolean equals(Object obj) Indicates whether some other object is “equal to” this Optional. |
3 |
Optional 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 |
Optional flatMap(Function super T,Optional> mapper) If a value is present, it applies the provided Optional-bearing mapping function to it, returns that result, otherwise returns an empty Optional. |
5 |
T get() If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException. |
6 |
int hashCode() Returns the hash code value of the present value, if any, or 0 (zero) if no value is present. |
7 |
void ifPresent(Consumer super T> consumer) If a value is present, it invokes the specified consumer with the value, otherwise does nothing. |
8 |
boolean isPresent() Returns true if there is a value present, otherwise false. |
9 |
Optional 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. |
10 |
static Returns an Optional with the specified present non-null value. |
11 |
static Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional. |
12 |
T orElse(T other) Returns the value if present, otherwise returns other. |
13 |
T orElseGet(Supplier extends T> other) Returns the value if present, otherwise invokes other and returns the result of that invocation. |
14 |
Returns the contained value, if present, otherwise throws an exception to be created by the provided supplier. |
15 |
String toString() Returns a non-empty string representation of this Optional suitable for debugging. |
此类从以下类继承方法-
使用您选择的任何编辑器(例如,C:\> JAVA)创建以下Java程序。
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 a = Optional.ofNullable(value1);
//Optional.of - throws NullPointerException if passed parameter is null
Optional b = Optional.of(value2);
System.out.println(java8Tester.sum(a,b));
}
public Integer sum(Optional a, Optional 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;
}
}
使用javac编译器编译类,如下所示:
C:\JAVA>javac Java8Tester.java
现在如下运行Java8Tester-
C:\JAVA>java Java8Tester
它应该产生以下输出-
First parameter is present: false
Second parameter is present: true
10