java8Optional 使用

一、示例

java 复制代码
public class TestMain {


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class Leader {

        private Long employeeId;
        private BigDecimal bonus;

    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class Employee {

        private Long id;
        private String name;
        private Boolean leader;

    }

    public static class FiltUtil {

        public void OptionalStudy() {
            Optional<Leader> leader = Optional.ofNullable(getEmployeeById(1L)
                    .filter(Employee::getLeader)
                    .map(Employee::getId)
                    .flatMap(this::getLeaderByEmployeeId)
                    .orElse(null));
            if (leader.isPresent()) {
                Optional.of(
                        leader.map(Leader::getBonus)
                                .map(bonus -> String.format("员工ID为1的leader奖金为:%s", bonus))
                                .orElse("员工ID为1的leader也没有奖金"))
                        .ifPresent(System.out::println);
            } else {
                System.out.println("员工ID为1的leader未找到,他可能只是一个基层员工,不配拥有奖金");
            }
        }

        private Optional<Employee> getEmployeeById(Long id) {
            //return Optional.of(new Employee(1L, "大老板", Boolean.TRUE));
            return Optional.of(new Employee(1L, "大老板", Boolean.FALSE));
        }

        private Optional<Leader> getLeaderByEmployeeId(Long employeeId) {
            //return employeeId == 1L ? Optional.of(new Leader(1L, BigDecimal.valueOf(1000000000))) : Optional.empty();
            return employeeId == 1L ? Optional.of(new Leader(1L, null)) : Optional.empty();
        }

    }

    public static void main(String[] args) {
//        FiltUtil filtUtil = new FiltUtil();
//        filtUtil.OptionalStudy();
        Employee employee = new Employee(null,"",true);
        //Optional.断言
//        Optional.ofNullable(employee.getId()).orElseThrow(()-> new IllegalArgumentException(String.format("%s人员的Id不能为NULL", employee.toString())));
        Optional.ofNullable(employee).filter(e-> StringUtil.isEmpty(e.getName())).orElseThrow(()-> new IllegalArgumentException(String.format("%s人员的name不能为空", employee.toString())));
        Optional.ofNullable(employee.getName()).orElseThrow(()-> new IllegalArgumentException(String.format("%s人员的name不能为空", employee.toString())));

        //Optional.非空判断
        Optional.ofNullable(employee).ifPresent(o -> o.setName("UNKNOW"));
        System.out.println(employee.toString());
    }
}
相关推荐
x***38163 小时前
springboot和springframework版本依赖关系
java·spring boot·后端
故事不长丨4 小时前
C#定时器与延时操作的使用
开发语言·c#·.net·线程·定时器·winform
hefaxiang4 小时前
C语言常见概念(下)
c语言·开发语言
S***84884 小时前
SpringSecurity踢出指定用户
java
p***s914 小时前
Spring数据库原理 之 DataSource
java·数据库·spring
adobehu4 小时前
麒麟系统安装jdk17
java·jdk
欧阳天风4 小时前
js实现鼠标横向滚动
开发语言·前端·javascript
spencer_tseng4 小时前
java.util.IllegalFormatPrecisionException
java·printf
虹科网络安全4 小时前
艾体宝干货 | Redis Java 开发系列#1 从零开始的环境搭建与实践指南
java·数据库·redis
铅笔侠_小龙虾4 小时前
Arthas 命令
java·jvm