java8 Optional 类
package jdk180reduce; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import test.Teacher; import test.User; public class optional { /** * 1.Optional的用法,可以,用了reduce计算,也可以不判断非null * 2.三个方法:Optional.empty(), Optional.of, Optional.ofNullable * 3.orElse()用法 * 4.ifPresent()区别isPresent() * 5.orElse和orElseGet()不同*/ public static void main(String[] args) { emptyOptional(); orelse(); orelseDifferent(); whenMap_thenOk(); List<Student> stuList = new ArrayList<>(4); stuList.add(new Student("刘一", 59)); stuList.add(new Student("陈二", 58)); stuList.add(new Student("张三", 98)); stuList.add(new Student("吴九", 100)); //计算分数在60分一下的分数总和 注意:以前没有60分以下的,不加判断就会出现空指针异常: score.orElse(0)= 有值取值,无值为0 Optional<Integer> score = stuList.stream().map(Student :: getScore).filter(s -> s<60).reduce((a,b) -> a+b); System.out.println("score="+score.orElse(0)); //0 Map<Integer,String> map = new HashMap<>(); map.put(20180001,"章子"); map.put(20180002,"小米"); map.put(20180003,"大黄"); String name = Optional.ofNullable(map.get(20180001)).orElse("无"); System.out.println(name); //无 } public static void emptyOptional() { // 以下会导致空指针:No value present // Optional<User> emptyOpt = Optional.empty(); // emptyOpt.get(); //当用.of的时候必须不为null,否则也会报空指针 User user = new User(); Optional<User> opt = Optional.of(user); opt.get(); //如果不确定传入的值是否为null,用ofNullable Object opf = Optional.ofNullable(null).orElse("null"); System.out.println(opf); //ofNullable方法也要isPresent判断是否有值,或者用.orElse赋值 String name = "John"; Optional<String> opu = Optional.ofNullable(null); if (opu.isPresent()) { System.out.println(opu.get()); } else { System.out.println("opu无值"); } //ifPresent区别isPresent,是用labam表达式 Optional<String> opp = Optional.ofNullable(name); opp.ifPresent(it -> System.out.println(opp.get())); } public static void orelse() { //orElse:如果有值则返回该值user,否则返回传递给它的参数值user2: User user = null; User user2 = new User(); user2.setUserId(2); User result = Optional.ofNullable(user).orElse(user2); System.out.println("result="+result); } /** * orElse和orElseGet()不同 */ public static void orelseDifferent() { User user = null; System.out.println("1----Using orElse"); User result = Optional.ofNullable(user).orElse(createNewUser()); System.out.println("1----Using orElseGet"); User result2 = Optional.ofNullable(user).orElseGet(() -> createNewUser()); User user4 = new User(); user4.setAge(2); System.out.println("2----Using orElse"); User result4 = Optional.ofNullable(user4).orElse(createNewUser()); System.out.println("result4====="+result4); System.out.println("2----Using orElseGet"); User result3 = Optional.ofNullable(user4).orElseGet(() -> createNewUser()); System.out.println("result3===="+result3); //结论:两个 Optional对象都包含非空值,两个方法都会返回对应的非空值。 //不过,orElse() 方法仍然创建了 User 对象。 与之相反,orElseGet() 方法不创建 User 对象。 //在执行较密集的调用时,比如调用 Web 服务或数据查询,这个差异会对性能产生重大影响。 } private static User createNewUser() { System.out.println("Creating New User"); User user = new User(); user.setName("java"); return user; } public static void whenMap_thenOk() { User user = createNewUser(); User userNull = new User(); String name = Optional.ofNullable(user).map(u -> u.getName()).orElse("vivi"); Optional<User> result = Optional.ofNullable(user).filter(u -> u.getName() != null && u.getName().contains("li")); result.ifPresent(u -> { String myName = result.get().getName(); //------------- System.out.println("过滤值:" + result.get().getName()); }); //第二种: if (result.isPresent()) { System.out.println("过滤值2:" + result.get().getName()); } } }
版权声明:本文为lgg20原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。