惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
B
Blog
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Jina AI
Jina AI
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
L
LangChain Blog
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
F
Fortinet All Blogs
H
Help Net Security
B
Blog RSS Feed
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题

博客园 - 刚泡

数据库短暂波动导致xxl-job-admin不调度任务的问题 CompletableFuture用法详解 使用责任链模式简化if-else代码示例 自定义AngularJS Modal弹框大小的方法 SpringCloud Alibaba 改造Sentinel Dashboard将熔断规则持久化到Nacos SpringCloud Alibaba 改造Sentinel Dashboard将流控规则持久化到Nacos [Access][Microsoft][ODBC 驱动程序管理器] 无效的字符串或缓冲区长度 Invalid string or buffer length Java操作Kafka执行不成功的解决方法,Kafka Broker Advertised.Listeners属性的设置 pom.xml错误:org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter)的解决方法 Spring Cloud @HystrixCommand和@CacheResult注解使用,参数配置 Spring Boot的属性加载顺序 Spring Boot 使用properties如何多环境配置 Spring Cloud微服务实战阅读笔记(一) 基础知识 Java使用POI为Excel打水印,调整列宽并设置Excel只读(用户不可编辑) Sitemesh 3使用及配置 Java上传文件夹(Jersey) 路径名导致的异常:javax.imageio.IIOException: Can't read input file! 解决安卓微信浏览器中上传不能调用相机的问题 Mac下安装SVN插件javaHL not available的解决方法
使用Function Interface简化if-else代码示例
刚泡 · 2025-09-25 · via 博客园 - 刚泡

使用表驱动的方法,利用Function Interface优化If-else的示例代码:

 1 package com.siasun.java8.function.function;
 2 
 3 import java.math.BigDecimal;
 4 import java.util.List;
 5 import java.util.function.Function;
 6 
 7 /**
 8  * 税金计算
 9  * Function Interface 简化 if-else判断示例
10  * @author : wanggang
11  * @create 2025/7/15 8:21
12  */
13 public class TaxCalculateDemo {
14 
15   //定义计算表达式
16   public static List<RangeHandler> calculateTaxHandlers = List.of(
17     new RangeHandler(
18       null,
19       new BigDecimal(36000),
20       d -> d.multiply(new BigDecimal("0.1"))
21     ),
22     new RangeHandler(
23       new BigDecimal(36000),
24       new BigDecimal(144000),
25       d -> d.multiply(new BigDecimal("0.2"))
26     ),
27     new RangeHandler(
28       new BigDecimal(14000),
29       null,
30       d -> d.multiply(new BigDecimal("0.3"))
31     )
32   );
33 
34   public static void main(String[] args) {
35     BigDecimal yearIncome = new BigDecimal(36001);
36 
37     BigDecimal tax = calculateTaxHandlers
38       .stream()
39       .filter(d -> d.matches(yearIncome))
40       .findFirst()
41       .map(handler -> handler.execute(yearIncome))
42       .orElse(new BigDecimal(0));
43 
44     System.out.println("个人所得税为:" + tax);
45   }
46 
47   public static class RangeHandler {
48 
49     private final BigDecimal min;
50     private final BigDecimal max;
51     private final Function<BigDecimal, BigDecimal> action;
52 
53     public RangeHandler(
54       BigDecimal min,
55       BigDecimal max,
56       Function<BigDecimal, BigDecimal> action
57     ) {
58       this.min = min;
59       this.max = max;
60       this.action = action;
61     }
62 
63     public boolean matches(BigDecimal value) {
64       boolean flag = true;
65       if (min != null) {
66         flag = value.compareTo(min) >= 0;
67       }
68       if (max != null) {
69         flag = flag && value.compareTo(max) < 0;
70       }
71       return flag;
72     }
73 
74     public BigDecimal execute(BigDecimal value) {
75       return action.apply(value);
76     }
77   }
78 }

使用责任链模式简化if-else代码示例