使用ImmutableMap简化语句
项目实战
最近接了一个出行权益的需求,回调的状态有十几种,需要转换为进行中,取消,已完成几种状态进行订单状态的展示,使用ImmutableMap可以简化语句,替代使用if-else 语句或者switch 语句。

ImmutableMap介绍
其中immutable[ɪˈmjuːtəbl],adj. 不变的;不可变的;不能变的。
使用场景
对于映射关系的 if-else 语句或者switch 语句,可以用Map来简化。
示例展示
使用switch
1 public static String getBiologyClass(String name) {
2 switch (name) {
3 case "dog" :
4 return "animal";
5 case "cat" :
6 return "animal";
7 case "lavender" :
8 return "plant";
9 ...
10 default :
11 return null;
12 }
13 }
使用ImmutableMap精简
1 private static final Map<String, String> BIOLOGY_CLASS_MAP
2 = ImmutableMap.<String, String>builder()
3 .put("dog", "animal")
4 .put("cat", "animal")
5 .put("lavender", "plant")
6 ...
7 .build();
8 public static String getBiologyClass(String name) {
9 return BIOLOGY_CLASS_MAP.get(name);
10 }
写法

好文
Java代码精简之道 --
使用ImmutableMap简化语句的更多相关文章
- shell之if简化语句
最常用的简化if语句: && 如果是“前面”,则“后面” [ -f /var/run/dhcpd.pid ] && rm /var/run/dhcpd.pid 检查 文 ...
- 一个sql很多个not like的简化语句
如: select * from table where `zongbu` not like '%北京%' and `zongbu` not like '%上海%' and `zongbu` not ...
- 【转载】shell编程——if语句 if -z -n -f -eq -ne -lt
shell编程中条件表达式的使用 if 条件then Commandelse Commandfi 别忘了这个结尾 If语句忘了结尾fites ...
- shell编程——if语句 if -z -n -f -eq -ne -lt
if 条件then Commandelse Commandfi 别忘了这个结尾 If语句忘了结尾fitest.sh: line 14: sy ...
- shell编程——if语句【转载】
(2)shell编程——if语句_macg_新浪博客http://blog.sina.com.cn/s/blog_6151984a0100ekl6.html shell编程——if语句转载 if 语句 ...
- shell编程——if语句
if 语句格式 if 条件 then Command else Command fi 别忘了这个结尾 If语句忘了结尾fi test.s ...
- Atitit.mysql oracle with as模式临时表模式 CTE 语句的使用,减少子查询的结构性 mssql sql server..
Atitit.mysql oracle with as模式临时表模式 CTE 语句的使用,减少子查询的结构性 mssql sql server.. 1. with ... as (...) 在mys ...
- Shell学习:if语句 if -z -n -f -eq -ne -lt
if 条件then Commandelse Commandfi 别忘了这个结尾 If语句忘了结尾fitest.sh: line 14: sy ...
- [Java编程思想-学习笔记]第3章 操作符
3.1 更简单的打印语句 学习编程语言的通许遇到的第一个程序无非打印"Hello, world"了,然而在Java中要写成 System.out.println("He ...
随机推荐
- 第五篇 Scrum 冲刺博客
一.站立式会议 1. 会议照片 2. 工作汇报 团队成员名称 昨日(26日)完成的工作 今天(27日)计划完成的工作 工作中遇到的困难 陈锐基 - 完成发布页面的布局- 完成发布动态的功能 - 优化当 ...
- 步步为营,打造CQUILib UI框架库
步步为营,打造CQUILib UI框架库 UI框架包括如下几个方面:: 丰富的UI控件 窗口管理 主题 多语言 托盘 视图与业务解耦 登录框效果如下:: 提示框效果如下:: 后续讲解如何步步为营,打造 ...
- 从 0 开始的min_max容斥证明
二项式反演 \[f_n=\sum\limits_{i=0}^nC^i_ng_i \Leftrightarrow g_n=\sum\limits_{i=0}^n{(-1)}^{n-i}f_i \] 证明 ...
- activiti环境安装
使用Eclipse安装activiti插件的时候,没有安装成功,参考这边文章才成功,链接:https://jingyan.baidu.com/article/4dc408480d4201c8d846f ...
- springboot配置ssl证书
springboot默认使用的是tomcat: 1.先到阿里云上注册一个证书,绑定域名:后面可以在管理中下载证书,下载tomcat对应的证书(一个*.pfx文件和*.txt文件) 2.将pfx文件拷贝 ...
- VMware 虚拟机网卡设置与外网访问
1 查看虚拟机网卡设置,查看虚拟机网关. 2 然后,设置本地机器VMnet8网卡的IP地址和子网掩码.切记,IP地址不能与虚拟机网卡IP地址相同. 3 配置虚拟机Centos的ens33网卡 TYPE ...
- 九、TestNG超时测试
"超时"表示如果单元测试花费的时间超过指定的毫秒数,那么TestNG将会中止它并将其标记为失败. 使用属性 timeOut = 参数(1s*1000) package com.lc ...
- Navicat Primium连接数据库报ORA-28547错误
这个问题主要是Navicat Primium与orecal中的oci.dll版本不一致造成的,无论是本地数据库或者网络数据库. 解决方法:在数据库orecal安装目录中搜索oci.dll文件,找到后将 ...
- vue 修改数据
通过数组中的方法改变数据 变异方法(改变原数组) push() pop() shift() unshift() splice() sort() reverse() 替换数组(生成新数组) filter ...
- 磁盘配额,通过ManagementClass("Win32_DiskQuota")
C# using System; using System.Collections.Generic; using System.Management; namespace ConsoleApp2 { ...