Java Optional 类

Java在JDK8中引入了一个新的Optional类。它是一个final类,用于处理Java应用程序中的NullPointerException。我们必须导入java.util包才能使用该类。Optional类提供了用于检查特定变量是否存在值的方法。

Java Optional 类的方法列表

Methods Description
public static <T> Optional<T> empty() It returns an empty Optional object. No value is present for this Optional.
public static <T> Optional<T> of(T value) It returns an Optional with the specified present non-null value.
public static <T> Optional<T> ofNullable(T value) It returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
public T get() If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
public boolean isPresent() It returns true if there is a value present, otherwise false.
public void ifPresent(Consumer<? super T> consumer) If a value is present, invoke the specified consumer with the value, otherwise do nothing.
public Optional<T> filter(Predicate<? super T> predicate) If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.
public <U> Optional<U> map(Function<? super T,? extends U> mapper) If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.
public <U> Optional<U> flatMap(Function<? super T,Optional<U> mapper) If a value is present, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional.
public T orElse(T other) It returns the value if present, otherwise returns other.
public T orElseGet(Supplier<? extends T> other) It returns the value if present, otherwise invoke other and return the result of that invocation.
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X extends Throwable It returns the contained value, if present, otherwise throw an exception to be created by the provided supplier.
public boolean equals(Object obj) Indicates whether some other object is "equal to" this Optional or not. The other object is considered equal if:
  • It is also an Optional and;
  • Both instances have no value present or;
  • the present values are "equal to" each other via equals().
public int hashCode() It returns the hash code value of the present value, if any, or returns 0 (zero) if no value is present.
public String toString() It returns a non-empty string representation of this Optional suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.

Java Optional类应用场景

在下面的示例中,我们没有使用可选类。此程序异常终止并引发nullPointerException。

public class Test
{
    public static void main(String[] args)
    {
        String[] str = new String[10];
        String lowercaseString = str[5].toLowerCase();
        System.out.print(lowercaseString);
    }
}
Exception in thread "main" java.lang.NullPointerException
    at Test.main(Test.java:8)

应用场景1

import java.util.Optional;

public class Test
{
    public static void main(String[] args)
    {
        String[] str = new String[10];
        Optional<String> checkNull = Optional.ofNullable(str[5]);
        if (checkNull.isPresent())
        {
            String lowercaseString = str[5].toLowerCase();
            System.out.print(lowercaseString);
        }
        else
            System.out.println("string value is not present");
    }
}

输出结果为:

string value is not present

应用场景2

import java.util.Optional;

public class Test
{
    public static void main(String[] args)
    {
        String[] str = new String[10];
        str[5] = "JAVA OPTIONAL CLASS EXAMPLE";
        Optional<String> checkNull = Optional.ofNullable(str[5]);
        if (checkNull.isPresent())
        {
            String lowercaseString = str[5].toLowerCase();
            System.out.print(lowercaseString);
        }
        else
        {
            System.out.println("String value is not present");
        }
    }
}

输出结果为:

java optional class example

标签: none

[站长收徒]-[Java架构师学习指导]-[最后一次]


添加新评论