lambda -- Filter Java Stream to 1 and only 1 element
|
I am trying to use Java 8 Take this code:
This code finds a Changing the filter line to:
Will throw a I would like it to throw an error if there are multiple matches, though. Is there a way to do this? |
|||||||||||||||||
|
|
Technically there's an ugly 'workaround' that involves What I do in these cases is just collecting it in a list, like this:
I am not aware of a way to do this in the API, meanwhile I will work on another example involving a custom element. Update, You should create your own
What it does is:
Used as:
You can then customize this New update, I revised my old answer once more for |
|||||||||||||||||
|
|
Wow, such complexity! :-) The other answers that involve writing a custom Collector are probably more efficient (such as Louis Wasserman's, +1), but if you want brevity, I'd suggest the following:
Then verify the size of the result list. |
|||||||||
|
|
You could roll your own Collector for this:
...or using your own |
|||||||||||||||||||||
|
|
The "escape hatch" operation that lets you do weird things that are not otherwise supported by streams is to ask for an
Guava has a convenience method to take an |
||||
|
The exception is thrown by
which throws a Or you could use a reduction combined with an optional:
The reduction essentially returns:
The result is then wrapped in an optional. But the simplest solution would probably be to just collect to a collection, check that its size is 1 and get the only element. |
|||||||||
|
|
Have you tried this
|
|||||
|
lambda -- Filter Java Stream to 1 and only 1 element的更多相关文章
- Java 8 Lambda 表达式及 Stream 在集合中的用法
简介 虽然 Java 8 已经发布有一段时间了,但是关于 Java 8 中的 Lambda 表达式最近才开始系统的学习,刚开始就被 Stream 的各种骚操作深深的吸引住了,简直漂亮的不像 Java. ...
- Java Stream 使用详解
Stream是 Java 8新增加的类,用来补充集合类. Stream代表数据流,流中的数据元素的数量可能是有限的,也可能是无限的. Stream和其它集合类的区别在于:其它集合类主要关注与有限数量的 ...
- 初探Lambda表达式/Java多核编程【1】从集合到流
从集合到流 接上一小节初探Lambda表达式/Java多核编程[0]从外部迭代到内部迭代,本小节将着手使用"流"这一概念进行"迭代"操作. 首先何为" ...
- 初探Lambda表达式/Java多核编程【2】并行与组合行为
今天又翻了一下书的目录,第一章在这之后就结束了.也就是说,这本书所涉及到的新的知识已经全部点到了. 书的其余部分就是对这几个概念做一些基础知识的补充以及更深层次的实践. 最后两个小节的内容较少,所以合 ...
- 十分钟学会Java8:lambda表达式和Stream API
Java8 的新特性:Lambda表达式.强大的 Stream API.全新时间日期 API.ConcurrentHashMap.MetaSpace.总得来说,Java8 的新特性使 Java 的运行 ...
- JDK1.8中的Lambda表达式和Stream
1.lambda表达式 Java8最值得学习的特性就是Lambda表达式和Stream API,如果有python或者javascript的语言基础,对理解Lambda表达式有很大帮助,因为Java正 ...
- Java Stream函数式编程案例图文详解
导读 作者计划把Java Stream写成一个系列的文章,本文只是其中一节.更多内容期待您关注我的号! 一.什么是Java Stream? Java Stream函数式编程接口最初是在Java 8中引 ...
- Java Stream函数式编程图文详解(二):管道数据处理
一.Java Stream管道数据处理操作 在本号之前发布的文章<Java Stream函数式编程?用过都说好,案例图文详解送给你>中,笔者对Java Stream的介绍以及简单的使用方法 ...
- Java Stream函数式编程第三篇:管道流结果处理
一.Java Stream管道数据处理操作 在本号之前写过的文章中,曾经给大家介绍过 Java Stream管道流是用于简化集合类元素处理的java API.在使用的过程中分为三个阶段.在开始本文之前 ...
随机推荐
- jquery中Live方法不可用,Jquery中Live方法失效
jquery中Live方法不可用,Jquery中Live方法失效 >>>>>>>>>>>>>>>>> ...
- C#发送邮件-C#教程
如何利用C#实现邮件发送功能?闲话不多说请看代码: public static void SendMail(MyEmail email){//发送验证邮箱邮件.//1.创建邮件MailMessage ...
- HTML 转义字符
在HTML中,一个包含特殊字符(如<>&)的字符串,要显示在页面上,由于添加到文本节点时会被认为是HTML的标签结构,造成一些错误,因此,要将这些特殊字符进行转义. 例如在< ...
- ==和equals
- Graphics类绘制图形
1. 画直线 void drawLine(int startX,int startY,int endX,int endY); 四个参数分别为:起始点的x坐标和y坐标以及终点的x坐标和y坐标,该方法用于 ...
- python中os模块的常用接口和异常中Exception的运用
1.os.path.join(arg1, arg2) 将arg1和arg2对应的字符串连接起来并返回连接后的字符串,如果arg1.arg2为变量,就先将arg1.arg2转换为字符串后再进行连接. 2 ...
- WPF扩展标记
标记扩展和 WPF XAML,标记扩展是 XAML 语言以及 XAML 服务的 .NET 实现的常规功能 XAML 处理器和标记扩展 XAML 分析器可将特性值解释为可转换成基元的文本字符串,或可通过 ...
- 转】VB中Set的用法
Set 语句 将对象引用赋给变量或属性. 语法 Set objectvar = {[New] objectexpression | Nothing} Set 语句的语法包含下面部分: 部分 描述 ob ...
- ligerUI路径问题
ligerUI放mv的Content目录下,路径为固定的并且必须引进一下文件 <link href="~/Content/Ligerui/Source/lib/ligerUI/skin ...
- android小知识
string 与 []byte 互转: public String BytesToString(byte[] data) { return new String(data); } public byt ...




