JDK8新特性之Optional
轉(zhuǎn)載自?JDK8新特性之Optional
?
Optional是什么
java.util.Optional
Jdk8提供 Optional,一個(gè)可以包含null值的容器對(duì)象,可以用來代替xx != null的判斷。
Optional常用方法
of
public static <T> Optional<T> of(T value) {return new Optional<>(value); } static <T> Optional<T> of(T value) {return new Optional<>(value); }為value創(chuàng)建一個(gè)Optional對(duì)象,如果value為空則 會(huì)報(bào)出NullPointerException異常。
ofNullable
public static <T> Optional<T> ofNullable(T value) {return value == null ? empty() : of(value); } static <T> Optional<T> ofNullable(T value) {return value == null ? empty() : of(value); }為value創(chuàng)建一個(gè)Optional對(duì)象,但可以允許value為null值。
isPresent
public boolean isPresent() {return value != null; } boolean isPresent() {return value != null; }判斷當(dāng)前value是否為null,如果不為null則返回true,否則false。
ifPresent
如果不為null值就執(zhí)行函數(shù)式接口的內(nèi)容。
public void ifPresent(Consumer<? super T> consumer) {if (value != null)consumer.accept(value); } void ifPresent(Consumer<? super T> consumer) {if (value != null)consumer.accept(value); }get
public T get() {if (value == null) {throw new NoSuchElementException("No value present");}return value; } T get() {if (value == null) {throw new NoSuchElementException("No value present");}return value; }返回當(dāng)前的值,如果為空則報(bào)異常。
orElse
返回當(dāng)前值,如果為null則返回other。
public T orElse(T other) {return value != null ? value : other; } T orElse(T other) {return value != null ? value : other; }orElseGet
orElseGet和orElse類似,只是orElseGet支持函數(shù)式接口來生成other值。
public T orElseGet(Supplier<? extends T> other) {return value != null ? value : other.get(); } T orElseGet(Supplier<? extends T> other) {return value != null ? value : other.get(); }orElseThrow
如果有值則返回,沒有則用函數(shù)式接口拋出生成的異常。
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {if (value != null) {return value;} else {throw exceptionSupplier.get();} } <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {if (value != null) {return value;} else {throw exceptionSupplier.get();} }示例
public static void main(String[] args) {testOf();testNullable(); } private static void testNullable() {User user = null;User john = new User("john", 18);User dick = new User("dick", 12); ? ?System.out.println(Optional.ofNullable(user).orElse(john));System.out.println(Optional.ofNullable(john).get());System.out.println(Optional.ofNullable(dick).orElse(john));System.out.println(Optional.ofNullable(user).orElseGet(() -> john)); ? ?System.out.println(); } private static void testOf() {try {User user1 = new User();Optional<User> userOptional1 = Optional.of(user1);if (userOptional1.isPresent()) {System.out.println("user is not null");} ? ? ? ?User user2 = null;Optional<User> userOptional2 = Optional.of(user2);//NullPointerExceptionif (userOptional2.isPresent()) {System.out.println("user is not null");}} catch (Exception e) {e.printStackTrace();}System.out.println(); } static void main(String[] args) {testOf();testNullable(); } private static void testNullable() {User user = null;User john = new User("john", 18);User dick = new User("dick", 12); ? ?System.out.println(Optional.ofNullable(user).orElse(john));System.out.println(Optional.ofNullable(john).get());System.out.println(Optional.ofNullable(dick).orElse(john));System.out.println(Optional.ofNullable(user).orElseGet(() -> john)); ? ?System.out.println(); } private static void testOf() {try {User user1 = new User();Optional<User> userOptional1 = Optional.of(user1);if (userOptional1.isPresent()) {System.out.println("user is not null");} ? ? ? ?User user2 = null;Optional<User> userOptional2 = Optional.of(user2);//NullPointerExceptionif (userOptional2.isPresent()) {System.out.println("user is not null");}} catch (Exception e) {e.printStackTrace();}System.out.println(); }Optional在jdk8中有大量使用,比如像Stream流中,但 Optional用在null判斷感覺也沒什么鳥用。。
在Spring4中也可以用Optional來代替autowired(require=false)的情況,參考?xì)v史Spring系列文章。
?
總結(jié)
以上是生活随笔為你收集整理的JDK8新特性之Optional的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 14 源码正式推送至 A
- 下一篇: wifi正常苹果手机连上用不了