查找满足断言的第一个元素

问题:查找满足断言的第一个元素

我刚刚开始使用Java 8的lambdas,我尝试去实现一些我在函数式语言里面经常用的

例如,大部分的函数式语言里有一些查找函数,针对序列或者list进行操作,返回使得断言为真的第一个元素。我唯一知道可以在Java8里面实现的就是:

lst.stream().filter(x -> x > 5).findFirst()

但是这对于我来说似乎效率不高,这个filter会扫描整个list,至少在我的理解上是这样的,这有可能是错误的,是不是有更好的方法。

回答一

return dataSource.getParkingLots().stream().filter(parkingLot -> Objects.equals(parkingLot.getId(), id)).findFirst().orElse(null);

我能filter出在对象列表里面唯一的那个对象,所以我经常用这个,希望可以帮到你吧

回答二

不是的,filter不用扫描整个stream的。它是一个中间操作,会返回一个lazy stream(事实上,所有的中间操作都会返回一个lazy stream)。为了说服你,你可以做一下下面的测试

List<Integer> list = Arrays.asList(1, 10, 3, 7, 5);
int a = list.stream().peek(num -> System.out.println("will filter " + num)).filter(x -> x > 5).findFirst().get();
System.out.println(a);

输出:

will filter 1
will filter 10
10

你可以看到它只处理了流的前两个元素。所以你可以采取这个非常好的方法的

回答三

@AjaxLeung已经回答了, 但是已经很难在评论中找到了.
只是用来检查

lst.stream().filter(x -> x > 5).findFirst().isPresent()

可以被简化为

lst.stream().anyMatch(x -> x > 5)

文章翻译自Stack Overflow:https://stackoverflow.com/questions/23696317/find-first-element-by-predicate

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390985.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Lock和synchronized的选择

学习资源:http://www.cnblogs.com/dolphin0520/p/3923167.html 一.java.util.concurrent.locks包下常用的类 1.Lock public interface Lock { void lock();//用来获取锁。如果锁已被其他线程获取&#xff0c;则进行等待。void lockInterruptibly() throws InterruptedException…

python 面试问题_值得阅读的30个Python面试问题

python 面试问题Interview questions are quite tricky to predict. In most cases, even peoples with great programming ability fail to answer some simple questions. Solving the problem with your code is not enough. Often, the interviewer will expect you to hav…

spring boot中 使用http请求

因为项目需求&#xff0c;需要两个系统之间进行通信&#xff0c;经过一番调研&#xff0c;决定使用http请求。服务端没有什么好说的&#xff0c;本来就是使用web 页面进行访问的&#xff0c;所以spring boot启动后&#xff0c;controller层的接口就自动暴露出来了&#xff0c;客…

arduino joy_如何用Joy开发Kubernetes应用

arduino joyLet’s face it: Developing distributed applications is painful.让我们面对现实&#xff1a;开发分布式应用程序很痛苦。 Microservice architectures might be great for decoupling and scalability but they are intimidatingly complex when it comes to de…

怎么样得到平台相关的换行符?

问题&#xff1a;怎么样得到平台相关的换行符&#xff1f; Java里面怎么样得到平台相关的换行符。我不可能到处都用"\n" 回答一 In addition to the line.separator property, if you are using java 1.5 or later and the String.format (or other formatting me…

scrapy常用工具备忘

scrapy常用的命令分为全局和项目两种命令&#xff0c;全局命令就是不需要依靠scrapy项目&#xff0c;可以在全局环境下运行&#xff0c;而项目命令需要在scrapy项目里才能运行。一、全局命令##使用scrapy -h可以看到常用的全局命令 [rootaliyun ~]# scrapy -h Scrapy 1.5.0 - n…

机器学习模型 非线性模型_机器学习:通过预测菲亚特500的价格来观察线性模型的工作原理...

机器学习模型 非线性模型Introduction介绍 In this article, I’d like to speak about linear models by introducing you to a real project that I made. The project that you can find in my Github consists of predicting the prices of fiat 500.在本文中&#xff0c;…

NOIP赛前模拟20171027总结

题目&#xff1a; 1.寿司 给定一个环形的RB串要求经过两两互换后RB分别形成两段连续区域,求最少操作次数(算法时间O(n)) 2.金字塔 给定一个金字塔的侧面图有n层已知每一层的宽度高度均为1要求在图中取出恰好K个互不相交的矩形&#xff08;边缘可以重叠&#xff09;,求最多可以取…

虚幻引擎 js开发游戏_通过编码3游戏学习虚幻引擎4-5小时免费游戏开发视频课程

虚幻引擎 js开发游戏One of the most widely used game engines is Unreal Engine by Epic Games. On the freeCodeCamp.org YouTube channel, weve published a comprehensive course on how to use Unreal Engine with C to develop games.Epic Games的虚幻引擎是使用最广泛的…

建造者模式什么时候使用?

问题&#xff1a;建造者模式什么时候使用&#xff1f; 建造者模式在现实世界里面的使用例子是什么&#xff1f;它有啥用呢&#xff1f;为啥不直接用工厂模式 回答一 下面是使用这个模式的一些理由和Java的样例代码&#xff0c;但是它是由设计模式的4个人讨论出来的建造者模式…

TP5_学习

2017.10.27&#xff1a;1.index入口跑到public下面去了 2.不能使用 define(BIND_MODULE,Admin);自动生成模块了&#xff0c;网上查了下&#xff1a; \think\Build::module(Admin);//亲测,可用 2017.10.28:1.一直不知道怎么做查询显示和全部显示&#xff0c;原来如此简单&#x…

sql sum语句_SQL Sum语句示例说明

sql sum语句SQL中的Sum语句是什么&#xff1f; (What is the Sum statement in SQL?) This is one of the aggregate functions (as is count, average, max, min, etc.). They are used in a GROUP BY clause as it aggregates data presented by the SELECT FROM WHERE port…

10款中小企业必备的开源免费安全工具

10款中小企业必备的开源免费安全工具 secist2017-05-188共527453人围观 &#xff0c;发现 7 个不明物体企业安全工具很多企业特别是一些中小型企业在日常生产中&#xff0c;时常会因为时间、预算、人员配比等问题&#xff0c;而大大减少或降低在安全方面的投入。这时候&#xf…

为什么Java里面没有 SortedList

问题&#xff1a;为什么Java里面没有 SortedList Java 里面有SortedSet和SortedMap接口&#xff0c;它们都属于Java的集合框架和提供对元素进行排序的方法 然鹅&#xff0c;在我的认知里Java就没有SortedList这个东西。你只能使用java.util.Collections.sort()去排序一个list…

图片主成分分析后的可视化_主成分分析-可视化

图片主成分分析后的可视化If you have ever taken an online course on Machine Learning, you must have come across Principal Component Analysis for dimensionality reduction, or in simple terms, for compression of data. Guess what, I had taken such courses too …

回溯算法和递归算法_回溯算法:递归和搜索示例说明

回溯算法和递归算法Examples where backtracking can be used to solve puzzles or problems include:回溯可用于解决难题或问题的示例包括&#xff1a; Puzzles such as eight queens puzzle, crosswords, verbal arithmetic, Sudoku [nb 1], and Peg Solitaire. 诸如八个皇后…

C#中的equals()和==

using System;namespace EqualsTest {class EqualsTest{static void Main(string[] args){//值类型int x 1;int y 1;Console.WriteLine(x y);//TrueConsole.WriteLine(x.Equals(y));//True //引用类型A a new A();B b new B();//Console.WriteLine(ab);//报错…

JPA JoinColumn vs mappedBy

问题&#xff1a;JPA JoinColumn vs mappedBy 两者的区别是什么呢 Entity public class Company {OneToMany(cascade CascadeType.ALL , fetch FetchType.LAZY)JoinColumn(name "companyIdRef", referencedColumnName "companyId")private List<B…

TP引用样式表和js文件及验证码

TP引用样式表和js文件及验证码 引入样式表和js文件 <script src"__PUBLIC__/bootstrap/js/jquery-1.11.2.min.js"></script> <script src"__PUBLIC__/bootstrap/js/bootstrap.min.js"></script> <link href"__PUBLIC__/bo…

pytorch深度学习_深度学习和PyTorch的推荐系统实施

pytorch深度学习The recommendation is a simple algorithm that works on the principle of data filtering. The algorithm finds a pattern between two users and recommends or provides additional relevant information to a user in choosing a product or services.该…