Java 1.5 是否有相当于 Predicate的方法? .Net 中的方法?

发布于 2024-07-18 03:47:55 字数 155 浏览 10 评论 0原文

具体来说,我正在寻找与 Collection.TrueForAll / 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萤火眠眠 2024-07-25 03:47:55

谓词 Google 集合库中提供。

Predicates are provided in the Google Collections library.

眼眸 2024-07-25 03:47:55

Functional Java 提供一流的函数。 谓词表示为F。 例如,下面的程序测试数组中是否存在全小写字母的字符串。

import fj.F;  
import fj.data.Array;  
import static fj.data.Array.array;
import static fj.function.Strings.matches;

public final class List_exists {  
  public static void main(final String[] args) { 
    final Array<String> a = array("Hello", "There", "how", "ARE", "yOU?");  
    final boolean b = a.exists(matches.f("^[a-z]*$"));  
    System.out.println(b); // true
  }  
}

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.

import fj.F;  
import fj.data.Array;  
import static fj.data.Array.array;
import static fj.function.Strings.matches;

public final class List_exists {  
  public static void main(final String[] args) { 
    final Array<String> a = array("Hello", "There", "how", "ARE", "yOU?");  
    final boolean b = a.exists(matches.f("^[a-z]*$"));  
    System.out.println(b); // true
  }  
}
清风不识月 2024-07-25 03:47:55

据我所知,没有。 但是 Apache Commons Collections 有这样的东西:谓词


编辑:右,如评论中所述,Commons Collections 来自前泛型世界,因此 Google 集合更新Guava)现在看起来显然是更好的选择。 尽管如此,Commons Collections 仍然值得一提,因为它是一个知名的图书馆,它做到了这一点,而且还让人们知道为什么不使用它。 :)

我刚刚在这篇精彩访谈中阅读了有关 Google Collections 的更多信息开发人员,并想引用一些专门处理“Google Collections 与 Apache Commons Collections”问题的内容:

您的方法有何独特之处?
它有何不同,例如
Apache Commons Collection?

凯文:“好吧,感谢上帝赐予阿帕奇
公共资源。 我们都会处于糟糕的状态
没有这样的库。 那
遗憾的是,那个特定项目
已经停滞在前仿制药的世界中。
他们确实想采用仿制药,但是
他们认识到这将涉及
一个非常重要且不兼容的
改写。 到目前为止,似乎还没有人
积极推动这一努力。 在
Google 我们一直在使用 Java 5
自 2005 年春季起在全公司范围内实施。
一个馆藏图书馆正在
未能量化是一个交易破坏者
我们,因为我们真的讨厌得到
编译器警告。 我当时也是
关心很多地方
Apache 集合没有
符合规范
他们实现的 JDK 接口。”

[...]

贾里德:“正如凯文暗示的那样,我们的图书馆
是我所知道的唯一的馆藏图书馆
的,在 JDK 之外,使用 Java 5 构建
特征:泛型、枚举、协变
返回类型等。编写 Java 5 时
代码,你想要一个集合库
充分利用了
语言。 此外,我们投入了大量
努力打造图书馆
完整、稳健且一致
JDK 集合类。 我们的
集合类更多
最初有限,但我们逐渐
在过去的两年里改进了它们。
由于所有库的使用都在 Google 中
源控制系统,我们已经有了
灵活修改公共
接口。 一个开源项目
就像 Apache Commons Collection 不一样
有改变其的自由
首次发布后的行为。
因为我们一旦失去灵活性
Google Collections Library 1.0 是
已发布,我们渴望收到
现在反馈,以便我们得到东西
对。”

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:

What is unique about your approach?
How does it differ to, for example,
the Apache Commons Collection?

Kevin: "Well, thank God for the Apache
Commons. We'd all be in bad shape
without libraries like this. That
said, sadly that particular project
has stalled, in a pre-generics world.
They do want to adopt generics, but
they recognize that this would involve
a pretty nontrivial and incompatible
rewrite. So far, no one seems to be
actively driving such an effort. At
Google we've been using Java 5
company-wide since the spring of 2005.
A collections library being
ungenerified was a deal-breaker for
us, because we really hate getting
compiler warnings. I was also
concerned about the many places in
which the Apache collections don't
conform to the specifications of the
JDK interfaces they implement."

[...]

Jared: "As Kevin implies, our library
is the only collections library I know
of, outside the JDK, built with Java 5
features: generics, enums, covariant
return types, etc. When writing Java 5
code, you want a collections library
that takes full advantage of the
language. In addition, we put enormous
effort into making the library
complete, robust, and consistent with
the JDK collection classes. Our
collection classes were much more
limited initially, but we've gradually
improved them over the last two years.
Since all library usage is in Google's
source control system, we've had the
flexibility to modify public
interfaces. An open-source project
like Apache Commons Collection doesn't
have the freedom to change its
behavior after the initial release.
Since we'll lose that flexibility once
Google Collections Library 1.0 is
released, we're eager to receive
feedback now so we can get things
right."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文