使用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 ...
随机推荐
- Day4 【Scrum 冲刺博客】
每日会议总结 昨天已完成的工作 方晓莹(PIPIYing) 完善人员管理页的未完成部分 方子茵(Laa-L):无 黄芯悦(Sheaxx) 开始投诉反馈页面的开发 舒雯钰(LittleTaro) 博客的 ...
- HTML5中的自定义属性data-*
在html5中,给元素添加自定义属性需要用到data-*,比如data-name,添加完data-自定义属性之后通过元素的dataset属性来访问其值. dataset与getAttribute/se ...
- 微软面试题:剑指 Offer 51. 数组中的逆序对 Hard 出现次数:3
题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对. 输入一个数组,求出这个数组中的逆序对的总数. 示例 1: 输入: [7,5,6,4] 输出: 5 限制: ...
- 学习笔记:舞蹈链 Dancing Links
这是一种奇妙的算法用来解决两个问题: 精确覆盖问题:给定一个矩阵,每行是一个二进制数,选出尽量少的行,使得每一列恰好有一个 \(1\) 重复覆盖问题:给定一个矩阵,每行是一个二进制数,选出尽量少的行, ...
- 【题解】CIRU - The area of the union of circles [SP8073] \ 圆的面积并 [Bzoj2178]
[题解]CIRU - The area of the union of circles [SP8073] \ 圆的面积并 [Bzoj2178] 传送门: \(\text{CIRU - The area ...
- vue seo 优化
预渲染prerender-spa-plugin 如果你只是用来改善少数营销页面(例如 /, /about, /contact 等)的 SEO,那么你可能需要预渲染.无需使用 web 服务器实时动态编译 ...
- 加快Linux上yum下载安装包的速度(以CentOS 7,安装gcc为例)
今天在学习Linux的过程中,学到了关于包的安装问题:rpm包管理和yum在线管理两种方式:这里因为我在实验yum安装gcc出现了网速超级慢的问题,于是搜索解决方案,重新配置repo得以解决,记录整个 ...
- [WPF] 在 ViewModel 中让数据验证出错(Validation.HasError)的控件获得焦点
1. 需求 在 MVVM 中 ViewModel 和 View 之间的交互通常都是靠 Icommand 和 INotifyPropertyChanged,不过有时候还会需要从 MVVM 中控制 Vie ...
- Python三个处理excel表格的库
三个向excel表格写入数的库:xlwt,xlsxwriter,openpyxl,代码如下: 1 #第一个库,xlwt,不能写超过256列的表格 2 import xlwt 3 4 #新建workbo ...
- python之解压序列并赋值给变量
N个数量的序列(可迭代对象),赋值给N个变量. 字符串: 1 #!usr/bin/env python3 2 # -*- Coding=utf-8 -*- 3 4 ''' 5 解压序列(或者任何可迭代 ...