Optional类
方法 empty()方法 of(T value) ofNullable(T value) filter(Predicate<? super T> predicate) get() ifPresent(Consumer<? super T> consumer) isPresent() map(Function<? super T,? extends U> mapper) orElse(T other) orElseGet(Supplier<? extends T> other) flatMap(Function<? super T,Optional<U>> mapper)
方法
empty()方法
返回一个空的 Optional实例。
public static void main ( String [ ] args) {
Optional < Object > empty = Optional . empty ( ) ;
System . out. println ( empty) ;
}
of(T value)
返回具有 Optional的当前非空值的Optional。如果传入空值会报空指针异常
public static void main ( String [ ] args) {
Optional < Object > empty = Optional . of ( new User ( "John" , "12345" ) ) ;
System . out. println ( empty) ;
}
ofNullable(T value)
返回一个 Optional指定值的Optional,如果为空,则返回一个空的 Optional 。
filter(Predicate<? super T> predicate)
如果一个值存在,并且该值给定的谓词相匹配时,返回一个 Optional描述的值,
否则返回一个空的 Optional 。
public static void main ( String [ ] args) {
Optional < User > optional = Optional . of ( new User ( "John" , "12345" ) ) ;
System . out. println ( optional) ;
Optional < User > user1 = optional. filter ( user -> "John" . equals ( user. getName ( ) ) ) ;
System . out. println ( user1) ;
Optional < User > user2 = optional. filter ( user -> "Jane" . equals ( user. getName ( ) ) ) ;
System . out. println ( user2) ;
}
get()
如果 Optional中有一个值,返回值,否则抛出 NoSuchElementException
Optional < User > user = Optional . of ( new User ( "John" , "123456" ) ) ;
System . out. println ( user. get ( ) ) ;
Optional < User > optional = Optional . empty ( ) ;
System . out. println ( optional. get ( ) ) ;
ifPresent(Consumer<? super T> consumer)
如果存在值,则使用该值调用指定的消费者,否则不执行任何操作。
Optional < User > user = Optional . of ( new User ( "John" , "123456" ) ) ;
user. ifPresent ( u -> System . out. printf ( "User name: %s, password: %s" , u. getName ( ) , u. getPassword ( ) ) ) ;
Optional < User > optional = Optional . empty ( ) ;
optional. ifPresent ( u -> System . out. println ( "User is not present" ) ) ;
isPresent()
返回 true如果存在值,否则为 false 。
map(Function<? super T,? extends U> mapper)
如果存在一个值,则应用提供的映射函数,如果结果不为空,则返回一个 Optional结果的 Optional 。
Optional < User > user = Optional . of ( new User ( "John" , "123456" ) ) ;
Optional < String > s1 = user. map ( User :: getName ) ;
System . out. println ( s1) ;
Optional < User > optional = Optional . empty ( ) ;
Optional < String > s = optional. map ( User :: getName ) ;
System . out. println ( s) ;
orElse(T other)
如果Optional容器中无值,则返回other
public static void main ( String [ ] args) {
Optional < User > user = Optional . of ( new User ( "John" , "123456" ) ) ;
User unknown = user. orElse ( new User ( "Unknown" , "000000" ) ) ;
System . out. println ( unknown. getName ( ) ) ;
Optional < User > optional = Optional . empty ( ) ;
User unknown1 = optional. orElse ( new User ( "Unknown" , "000000" ) ) ;
System . out. println ( unknown1. getName ( ) ) ;
}
orElseGet(Supplier<? extends T> other)
返回值(如果存在),否则调用 other并返回该调用的结果。适合需要进行一些操作然后得到值的场景
public static void main ( String [ ] args) {
Optional < User > user = Optional . of ( new User ( "John" , "123456" ) ) ;
User unknown = user. orElseGet ( ( ) -> {
User user2 = new User ( ) ;
user2. setName ( "Unknown2" ) ;
user2. setPassword ( "000000" ) ;
return user2;
} ) ;
System . out. println ( unknown. getName ( ) ) ;
Optional < User > optional = Optional . empty ( ) ;
User unknown2 = optional. orElseGet ( ( ) -> {
User user2 = new User ( ) ;
user2. setName ( "Unknown2" ) ;
user2. setPassword ( "000000" ) ;
return user2;
} ) ;
System . out. println ( unknown2. getName ( ) ) ;
}
flatMap(Function<? super T,Optional> mapper)
如果一个值存在,应用提供的 Optional映射函数给它,返回该结果,否则返回一个空的 Optional 。
public static void main ( String [ ] args) {
Optional < String > user = Optional . of ( "John" ) ;
Optional < User > user1 = user. flatMap ( name -> Optional . of ( new User ( name, "password" ) ) ) ;
System . out. println ( user1. get ( ) . getPassword ( ) ) ;
Optional < String > empty = Optional . empty ( ) ;
Optional < User > password = empty. flatMap ( name -> Optional . of ( new User ( name, "password" ) ) ) ;
System . out. println ( password) ;
}