标签:代码 java 8 user col ble 复杂 obb class author
import java.util.Optional; /** * java 8新特性 * * @author wang-xiaoming * @date 2019年7月2日 */ public class OptionalTest { public static void ofNullable(String value) { Optional<String> opt = Optional.ofNullable(value); System.out.println("【ofNullable】,value:" + opt.get()); } public static void of(String value) { Optional<String> opt = Optional.of(value); System.out.println("【of】,value:" + opt.get()); } /** * 有值则返回值,没有则返回【指定属性】 * * @param value */ public static void orElse(String value) { String orElse = Optional.ofNullable(value).orElse("default"); System.out.println("【orElse】,value:" + orElse); } /** * 有值则返回值,没有则执行‘->’后的【执行方法】 * * @param value */ public static void orElseGet(String value) { String orElseGet = Optional.ofNullable(value).orElseGet(() -> "default"); System.out.println("【orElseGet】,value:" + orElseGet); } /** * 判断当前value是否为null,为null,返回false;不为null,返回true * * @param value */ public static void isPresent(String value) { boolean present = Optional.ofNullable(value).isPresent(); System.out.println("【isPresent】,result:" + present); } /** * ep:当抛出NPE时,可以自己指定异常 * * @param value */ public static void orElseThrow(String value) { String orElseThrow = Optional.ofNullable(value).orElseThrow(() -> new IllegalArgumentException()); System.out.println("【orElseThrow】,exception:" + orElseThrow); } /** * 转换值-map map() 对值应用(调用)作为参数的函数,然后将返回的值包装在 Optional 中。这就使对返回值进行链试调用的操作成为可能 * * @param user */ public static void map(User user) { String map = Optional.ofNullable(user).map(u -> u.getName()).orElse("转换值-map"); System.out.println("【map】,result:" + map); } /** * 转换值-flatMap 既然 getter 方法返回 String 值的 Optional,你可以在对 User 的 Optional 对象调用 flatMap() 时,用它作为参数 * * @param user */ public static void flatMap(User user) { String flatMap = Optional.ofNullable(user).flatMap(u -> u.getHobby()).orElse("转换值-flatMap"); System.out.println("【flatMap】,result:" + flatMap); } /** * 添加过滤 * * @param user */ public static void filter(User user) { boolean filter = Optional.ofNullable(user).filter(u -> u.getName() != null && u.getName().contains("m")).isPresent(); System.out.println("【filter】,result:" + filter); } /** * 简化if...else...? * * @param user * @param type */ public static void doCheck(User user, int type) { String countryName = ""; if (type == 0) { countryName = Optional.ofNullable(user).flatMap(u -> u.getAddress()).flatMap(u -> u.getCountry()) .map(c -> c.getName()).orElse("濮阳"); } else if (type == 1) { countryName = Optional.ofNullable(user).flatMap(User::getAddress).flatMap(Address::getCountry) .map(Country::getName).orElse("濮阳"); } System.out.println("【doCheck】,result:" + countryName); } public static final String value = null; public static final User user0 = new User(); public static void main(String[] args) { // of(null);// Exception in thread "main" java.lang.NullPointerException // ofNullable(null);// Exception in thread "main" java.util.NoSuchElementException: No value present // orElse(null);// 【orElse】,value:default // orElseGet(null);// 【orElseGet】,value:default // isPresent(null);// 【isPresent】,result:false // orElseThrow(null);// Exception in thread "main" java.lang.IllegalArgumentException // map(new User(6, null, "篮球-basketball"));// 【map】,result:转换值-map // flatMap(new User(6, "xm", null));// 【flatMap】,result:转换值-flatMap // filter(new User(6, "xx", "篮球-basketball"));// 【filter】,result:false // 正常情况下,拿出用户城市名称 /*if (user != null) { Address address = user.getAddress(); if (address != null) { Country country = address.getCountry(); if (country != null) { String isocode = country.getIsocode(); if (isocode != null) { isocode = isocode.toUpperCase(); } } } }*/ doCheck(new User(6, "xx", "篮球-basketball"), 0);// 【doCheck】,result:濮阳s doCheck(new User(6, "xx", "篮球-basketball"), 1);// 【doCheck】,result:濮阳s } } /** * 用户 * * @author wang-xiaoming * @date 2019年7月1日 */ class User { private Integer sno; private String name; private String hobby; private Address address; public Integer getSno() { return sno; } public void setSno(Integer sno) { this.sno = sno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Optional<String> getHobby() { return Optional.ofNullable(hobby); } public void setHobby(String hobby) { this.hobby = hobby; } public Optional<Address> getAddress() { return Optional.ofNullable(address); } public void setAddress(Address address) { this.address = address; } public User(Integer sno, String name, String hobby) { super(); this.sno = sno; this.name = name; this.hobby = hobby; } public User() { super(); } } /** * 地址 * * @author wang-xiaoming * @date 2019年7月1日 */ class Address { private Country country; public Optional<Country> getCountry() { return Optional.ofNullable(country); } public void setCountry(Country country) { this.country = country; } } /** * 城市 * * @author wang-xiaoming * @date 2019年7月1日 */ class Country { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
虽然参考网上有查到一些示例,但是感觉用到的地方有限,就上段代码中备注掉的传统非空判断,其实没有必要将每个对象取出,直接判断也行,不过为了代码简洁,新特性需要多加借鉴。
下面的这个例子倒是有让人眼前一亮的感觉,如有更多渐变代码编程实例,欢迎留言!
// 求出集合中小于4的个数 // int[] arr = {0, 2, 5, 8, -6}; Integer[] arr = {null, 2, 5, 8, -6}; List<Integer> list = Arrays.asList(arr); System.out.println(list.stream().filter(rand -> rand != null && rand < 4).count());
套用公司的一句话:我们的目标是简单、直接地解决问题,任何复杂的套路都是耍流氓!
httpClient 远程调用接口,java8新特性Optional
标签:代码 java 8 user col ble 复杂 obb class author
原文地址:https://www.cnblogs.com/huakaiyoushi/p/11121934.html