当前位置:网站首页>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方法,实际上是先执行静态代码块,这句话读懂,应该就不难理解了吧_
边栏推荐
- 收藏 | 22个短视频学习Adobe Illustrator论文图形编辑和排版
- [fishing artifact] UI library second change lowcode tool -- List part (I) design and Implementation
- Joint recruitment notice of ganfei research group of Wuhan University and xuzhenjiang research group of Nanchang University
- 使用 .NET 升级助手将NET Core 3.1项目升级为.NET 6
- Leetcode 2194. Cells within a range in Excel table (yes, solved)
- Double write consistency problem
- 【湖南大学】考研初试复试资料分享
- CAS乐观锁
- canvas 高级功能(下)
- Servlet API
猜你喜欢

'virtue and art' in the field of recurrent+transformer video recovery

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

About component value transfer

Which colleges are particularly easy to enter?

Anyone who watches "Meng Hua Lu" should try this Tiktok effect

Thinking about the probability of drawing cards in the duel link of game king

MySQL - server configuration related problems

The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.7 实例(存储分配程序)

Project training of Shandong University rendering engine system (III)

Read MHD and raw images, slice, normalize and save them
随机推荐
HEMA is the best representative of future retail
34- [go] golang channel knowledge points
JS monitors whether the user opens the screen focus
Anyone who watches "Meng Hua Lu" should try this Tiktok effect
generate pivot data 1
JS écoute si l'utilisateur allume le focus de l'écran
Leetcode 2194. Cells within a range in Excel table (yes, solved)
[BSP video tutorial] BSP video tutorial issue 17: single chip microcomputer bootloader topic, startup, jump configuration and various usage of debugging and downloading (2022-06-10)
generate pivot data 0
acwing 801. Number of 1 in binary (bit operation)
Understand go modules' go Mod and go sum
Cookies and sessions
Acwing 797 differential
The C programming language (version 2) notes / 8 UNIX system interface / 8.1 file descriptor
Leetcode 2194. Excel 表中某个范围内的单元格(可以,已解决)
The C programming language (version 2) notes / 8 UNIX system interface / 8.4 random access (lseek)
std::set compare
generate pivot data 2
33-【go】Golang sync.WaitGroup的用法—保证go协程执行完毕,主协程才退出
大规模实时分位数计算——Quantile Sketches 简史