如何检查java集合的所有元素是否符合某些条件? [英] how to check if all elements of java collection match some condition?

查看:97
本文介绍了如何检查java集合的所有元素是否符合某些条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ArrayList< Integer> 。我想检查列表中的所有元素是否大于或小于某个条件。我可以通过迭代每个元素来做到这一点。但我想知道Collection类中是否有任何方法可以获得答案,就像我们可以用 Collections.max()和<$ c $来查找最大值或最小值c> Collections.min()。

I have an ArrayList<Integer>. I want to check if all elements of the list are greater then or less then certain condition. I can do it by iterating on each element. But I want to know if there is any method in Collection class to get the answer like we can do to find maximum or minimum with Collections.max() and Collections.min() respectively.

推荐答案

如果你有java 8,请使用stream的 allMatch function(参考):

If you have java 8, use stream's allMatch function (reference):

 ArrayList<Integer> col = ...;
 col.stream().allMatch(i -> i>0); //for example all integers bigger than zero

这篇关于如何检查java集合的所有元素是否符合某些条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆