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());
    }
}
相关推荐
言乐6几秒前
JavaScript概括前端原理
开发语言·前端·javascript·python·ecmascript
liulilittle几秒前
短期内不存在通用 AI 工作流魔法
大数据·开发语言·c++·人工智能·llm·agent·tools
郝学胜-神的一滴17 分钟前
C++ Templates 03:手写一个Stack看透模板核心机制
开发语言·c++·产品运营·软件工程·产品经理
(Charon)31 分钟前
【C/C++面试】手写内存池:从空闲链表到内存申请与释放
java·开发语言·windows
EatFan33 分钟前
Spring Boot 4 迁移避坑清单:Jackson 3、starter 拆分与最低 JDK 口径核对(含若依/芋道/CRMEB 升级对照)
java·数据库·spring boot·spring boot 4·java 21·jakarta ee 11·jackson 3
栖凤33 分钟前
多 Agent 工作流实践:从单打独斗到协同作战
java·linux·服务器
派小心.34 分钟前
页面关闭前埋点丢失:sendBeacon验收
开发语言·php
一木 之林34 分钟前
DeepSeek Agent 开发
java·前端·人工智能
Java后端的Ai之路44 分钟前
Python 进阶探索25 - difflib模块之文本对比
开发语言·python·django·哈希算法·difflib
Evand J1 小时前
【MATLAB例程】三维快速扩展随机树(RRT)路径规划与到达角(AOA)定位算法|三维避障+定位仿真。附下载链接
开发语言·算法·matlab·路径规划·定位