Java 8小记 - Optional

栏目: 编程语言 · Java · 发布时间: 7年前

内容简介:Java 8小记 - Optional

本文主要介绍 Java 8Optional 的简单使用

Address

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address{
    private String province;
    private String city;
}

of + ofNullable

相关代码托管在 java8_demo

@Test(expected = NoSuchElementException.class)
public void emptyTest(){
   // 声明一个空的Optional对象
   Optional<Address> nameOptional = Optional.empty();
   // java.util.NoSuchElementException: No value present
   nameOptional.get();
}
    
@Test(expected = NullPointerException.class)
public void ofNullTest(){
   // 依据实例创建Optional对象
   Optional.of(new Address("Guangdong", "Zhongshan"));
   
   // Optional.ofNullable(null)返回Optional.empty()
   assertEquals(Optional.empty(), Optional.ofNullable(null));
   // java.lang.NullPointerException
   Optional.of(null);
}

map

  1. Stream.map 的定义: <R> Stream<R> map(Function<? super T, ? extends R> mapper)
  2. Optional.map 的定义: <U> Optional<U> map(Function<? super T, ? extends U> mapper)
  3. 上面两者定义非常类似, Optional 可看成 最多包含一个元素Stream

Java 8小记 - Optional

@Test
public void mapTest(){
   String province = "Guangdong";
   String city = "Zhongshan";
   Optional<Address> addressOptional = Optional.of(new Address(province, city));
   // mapper : Address -> String
   // Optional<Address> -> Optional<String>
   Optional<String> stringOptional = addressOptional.map(Address::getCity);
   assertTrue(stringOptional.isPresent());
   assertEquals(city, stringOptional.get());
}

flatMap

  1. Stream.flatMap 的定义: <R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
  2. Optional.flatMap 的定义: <U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper)

Java 8小记 - Optional

@Test
public void flatmapTest(){
   String province = "Guangdong";
   String city = "Zhongshan";
   Optional<Address> addressOptional = Optional.of(new Address(province, city));
   // 对于 mapper 为 T -> Optional<U>时,调用Optional.map,生成的是Optional<Optional<U>>
   Optional<Optional<String>> optionalOptional = addressOptional.map(address -> Optional.ofNullable(address.getCity()));
   // 对于 mapper 为 T -> Optional<U>时,调用Optional.map,生成的是Optional<U>,被扁平化
   Optional<String> stringOptional = addressOptional.flatMap(address -> Optional.ofNullable(address.getCity()));
   assertTrue(stringOptional.isPresent());
   assertEquals(city, stringOptional.get());
}

解引用

@Test(expected = UnsupportedOperationException.class)
public void dereferenceTest(){
   // get:最简单 + 最不安全
   Address address = addressOptional.get();
   assertNotNull(address);
   
   address = null;
   Optional<Address> emptyAddressOptional = Optional.ofNullable(address);
   
   String defaultValue = "Unknown";
   // orElse:设置默认值
   Address elseAddress = emptyAddressOptional.orElse(new Address(defaultValue, defaultValue));
   assertEquals(defaultValue, elseAddress.getProvince());
   assertEquals(defaultValue, elseAddress.getCity());
   
   // orElseGet:orElse的延迟调用版本
   Address elseGetAddress = emptyAddressOptional.orElseGet(Address::new);
   assertNull(elseGetAddress.getProvince());
   assertNull(elseGetAddress.getCity());
   
   // ifPresent:存在值则运行consumer,否则不进行任何操作
   emptyAddressOptional.ifPresent(System.out::println);
   
   // orElseThrow:不存在时,抛出异常
   emptyAddressOptional.orElseThrow(UnsupportedOperationException::new);
}

filter

@Test
public void filterTest(){
   assertTrue(addressOptional.filter(address -> address.getCity().contains("Z")).isPresent());
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数学拾遗

数学拾遗

加黑蒂 / 清华大学出版社 / 2004-8 / 49.00元

Beginning graduate students in mathematics and other quantitative subjects are expected to have a daunting breadth of mathematical knowledge ,but few have such a backgroud .This book will help stedent......一起来看看 《数学拾遗》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具