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.在使用的过程中分为三个阶段.在开始本文之前 ...
随机推荐
- 免费的手机号码归属地查询API接口文档
聚合数据手机号码归属四查询API接口,根据手机号码或手机号码的前7位,查询手机号码归属地信息,包括省份 .城市.区号.邮编.运营商和卡类型. 通过链接https://www.juhe.cn/docs/ ...
- [GDI+] 生成缩略图的类文件SmallImage (转载)
直接看代码吧,大家可以直接复制使用 /// <summary> /// 类说明:SmallImage类, /// 编码日期:2012-08-20 /// 编 码 人: 苏飞 /// 联系方 ...
- JS 定時刷新父類頁面
function timeCount() { var url = "MAC.aspx"; parent.location.href = url; } function beginC ...
- C#获取数组的行和列数程序代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- java动态绑定的情况分析
java是面向对象的语言,java中多态的一种情况是动态绑定.所谓的动态绑定,分两种情况:当调用类方法的时候,java虚拟机会基于对象的引用类型来选择执行方法.当java调用一个实例方法的时候,他会根 ...
- MVVM模式应用 之在ViewModel中使用NavigationService
在ViewModel.cs页面中是不能使用NavigationService,那该怎么实现跳转呢? 其实在ViewModel中实现页面的跳转也很简单,下面的代码: using Microsoft.Ph ...
- createjs 下雪 实例
demo: http://output.jsbin.com/davixatona <!DOCTYPE html> <html> <head> <meta c ...
- Rendering Transparent 3D Surfaces in WPF with C#(转载)
Rendering Transparent 3D Surfaces in WPF with C# The primary problems that arise when rendering semi ...
- bootstrap 下 标签页跳转总结
最近遇到一个问题,是关于bootstrap中的标签页实现上的一些功能实现,现总结一下. 问题描述:点击其他标签页后,如何在点击搜索按钮后自动跳转到第一个标签页.如下图 通过对bootstrap框架里的 ...
- highcharts-Highmaps 动态传入城市名称
做前端按地区(地图)分布监控数据展示用了 HIGHMAPS JAVASCRIPT MAPS 控件,很好很强大. 基础实现是这样的:调用插件动态传入需要展示的数据(data),插件会在地图数据(mapd ...




