当前位置:网站首页>key为断言的map是怎么玩的
key为断言的map是怎么玩的
2022-06-12 16:32:00 【怎么又有bug单】
前言
在进入主题前,我们首先普及一下什么是断言,因为这也是java8的新特性,有些小伙伴可能还不清楚。
而map,我们就不多解释了。。
什么是断言
传统的断言
断言(assertion)是一种在程序中的一阶逻辑(如:一个结果为真或假的逻辑判断式),目的为了表示与验证软件开发者预期的结果——当程序执行到断言的位置时,对应的断言应该为真。若断言不为真时,程序会中止执行,并给出错误信息。
编写代码时,我们总是会做出一些假设,断言就是用于在代码中捕捉这些假设。程序员相信在程序中的某个特定点该表达式值为真,可以在任何时候启用和禁用断言验证,因此可以在测试时启用断言而在部署时禁用断言。同样,程序投入运行后,最终用户在遇到问题时可以重新启用断言。
使用断言可以创建更稳定、品质更好且 不易于出错的代码。当需要在一个值为FALSE时中断当前操作的话,可以使用断言。单元测试必须使用断言(Junit/JunitX)。
除了类型检查和单元测试外,断言还提供了一种确定各种特性是否在程序中得到维护的极好的方法。
使用断言使我们向按契约式设计更近了一步。
断言可以有两种形式
1.assert Expression1
2.assert Expression1:Expression2
其中Expression1应该总是一个布尔值,Expression2是断言失败时输出的失败消息的字符串。如果Expression1为假,则抛出一个 AssertionError,这是一个错误,而不是一个异常,也就是说是一个不可控制异常(unchecked Exception),AssertionError由于是错误,所以可以不捕获,但不推荐这样做,因为那样会使你的系统进入不稳定状态。
java中的断言
java中的断言也有assert,但其实我们这里想讲的是Predicate,这是一个判断是否满足条件的函数
代码怎么写?
看了如上这么多概念,可能读者还是不太理解我想表达什么,那我们直接上代码:
首先是service:
public class TestStaticService {
private static final Map<Predicate<Integer>, String> BIZ_TYPE_DECIDE_MAP = Maps.newLinkedHashMap();
static {
BIZ_TYPE_DECIDE_MAP.put(integer -> integer.equals(1), "这是1");
BIZ_TYPE_DECIDE_MAP.put(integer -> integer.equals(2), "这是2");
BIZ_TYPE_DECIDE_MAP.put(integer -> integer.equals(3), "这是3");
}
public String doService(Integer integer){
Optional<Map.Entry<Predicate<Integer>, String>> first =
BIZ_TYPE_DECIDE_MAP.entrySet().stream().filter(entry -> entry.getKey().test(integer)).findFirst();
return first.get().getValue();
}
}
调用的测试类:
@Test
public void test45(){
TestStaticService testStaticService = new TestStaticService();
System.out.println(testStaticService.doService(2));
}
结果是,输出:“这是2”
这段代码最重要的点在于:
表面上是调用doService方法,实际上是先执行静态代码块,这句话读懂,应该就不难理解了吧_
边栏推荐
- 'virtue and art' in the field of recurrent+transformer video recovery
- calibration of sth
- acwing 803. Interval merging
- The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.2 低级 I/O(read 和 write)
- acwing 2816. Judgement subsequence
- Multimix: small amount of supervision from medical images, interpretable multi task learning
- The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.3 open、creat、close、unlink
- Contract awarding and AQS
- Statistical machine learning code set
- acwing 801. Number of 1 in binary (bit operation)
猜你喜欢

收藏 | 22个短视频学习Adobe Illustrator论文图形编辑和排版

Which colleges are particularly easy to enter?

Recurrent+Transformer 视频恢复领域的‘德艺双馨’

pbootcms的if判断失效直接显示标签怎么回事?

HEMA is the best representative of future retail

Contract awarding and AQS

Project training of Shandong University rendering engine system (III)

Mongodb learning and sorting (basic command learning of users, databases, collections and documents)

The market share of packaged drinking water has been the first for eight consecutive years. How does this brand DTC continue to grow?

generate pivot data 0
随机推荐
js监听用户是否打开屏幕焦点
AssertJ 的异常(Exception )断言
Sum of acwing796 submatrix
武汉大学甘菲课题组和南昌大学徐振江课题组联合招聘启事
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.6 实例(目录列表)
[research] reading English papers -- the welfare of researchers in English poor
The market share of packaged drinking water has been the first for eight consecutive years. How does this brand DTC continue to grow?
<山东大学项目实训>渲染引擎系统(八-完)
Batch --03---cmdutil
关于组件传值
Project training of Shandong University rendering engine system (III)
generate pivot data 1
\begin{algorithm} 笔记
Leetcode 2190. 数组中紧跟 key 之后出现最频繁的数字(可以,一次过)
Gopher to rust hot eye grammar ranking
<山东大学项目实训>渲染引擎系统(五)
34- [go] golang channel knowledge points
33-【go】Golang sync.WaitGroup的用法—保证go协程执行完毕,主协程才退出
<山东大学项目实训>渲染引擎系统(二)
generate pivot data 1