Java 1.5 是否有相当于 Predicate的方法? .Net 中的方法?
具体来说,我正在寻找与 Collection
/ Exists
等类似的干净符号。
必须编写一个 foreach 循环来检查每个对象都返回一个方法,所以我希望有一个更好的 Java 习惯用法。
Specifically, I'm looking for similarly clean notation to the Collection<T>.TrueForAll
/ Exists
, etc.
It feels smelly to have to write a foreach loop to inspect the return of a method on each object, so I'm hoping there's a better Java idiom for it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
谓词 Google 集合库中提供。
Predicates are provided in the Google Collections library.
Functional Java 提供一流的函数。 谓词表示为
F
。 例如,下面的程序测试数组中是否存在全小写字母的字符串。Functional Java provides first-class functions. A predicate is expressed as
F<T, Boolean>
. For example, here's a program that tests an array for the existence of a string that is all lowercase letters.据我所知,没有。 但是 Apache Commons Collections 有这样的东西:
谓词
编辑:右,如评论中所述,Commons Collections 来自前泛型世界,因此
Google 集合(更新:Guava)现在看起来显然是更好的选择。 尽管如此,Commons Collections 仍然值得一提,因为它是一个知名的图书馆,它做到了这一点,而且还让人们知道为什么不使用它。 :)我刚刚在这篇精彩访谈中阅读了有关 Google Collections 的更多信息开发人员,并想引用一些专门处理“Google Collections 与 Apache Commons Collections”问题的内容:
As far as I know, no. But Apache Commons Collections has something like this:
Predicate
Edit: Right, as noted in comments, Commons Collections is from pre-generics world, so
Google Collections(update: Guava) seems like a clearly better option now. Still, Commons Collections deserves to be mentioned as it's a well-known library that does this, and also so that people know why not to use it. :)I was just reading more about Google Collections in this nice interview with its main developers, and wanted to quote a bit that deals specifically with the "Google Collections vs. Apache Commons Collections" issue: