总是返回 true 的内置 Java 8 谓词?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26549659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:46:30  来源:igfitidea点击:

Built-in Java 8 predicate that always returns true?

javalambdajava-8predicate

提问by Garret Wilson

Google Guava has a predicate that always returns true. Does Java 8 have something similar for its Predicate? I know I could use (foo)->{return true;}, but I want something pre-made, analogous to Collections.emptySet().

Google Guava 有一个总是返回true的谓词。Java 8 有类似的东西Predicate吗?我知道我可以使用(foo)->{return true;},但我想要一些预制的,类似于Collections.emptySet().

采纳答案by Stuart Marks

There are no built-in always-true and always-false predicates in Java 8. The most concise way to write these is

Java 8 中没有内置的 always-true 和 always-false 谓词。编写这些谓词最简洁的方法是

x -> true

and

x -> false

Compare these to

比较这些

Predicates.alwaysTrue() // Guava

and finally to an anonymous inner class:

最后到一个匿名内部类:

new Predicate<Object>() {
    public boolean test(Object x) {
        return true;
    }
}

Probably the reason that Guava has these built-in predicates is that there is a huge syntactic advantage of a static method call over an anonymous inner class. In Java 8, the lambda syntax is so concise that there is a syntactic disadvantageto writing out a static method call.

Guava 具有这些内置谓词的原因可能是静态方法调用比匿名内部类具有巨大的语法优势。在 Java 8 中,lambda 语法非常简洁,以至于写出静态方法调用在语法上存在缺陷

That's just a syntactic comparison, though. There is probably a small space advantage if there were a single global always-true predicate, compared to x -> trueoccurrences spread across multiple classes, each of which would create its own predicate instance. Is this what you're concerned about? The savings didn't seem compelling, which is probably why they weren't added in the first place. But it could be reconsidered for a future release.

不过,这只是一个句法比较。与x -> true分布在多个类中的事件相比,如果存在单个全局始终为真谓词,则可能有很小的空间优势,每个类都会创建自己的谓词实例。这是你关心的吗?节省的费用似乎并不引人注目,这可能是最初没有添加它们的原因。但可以在未来的版本中重新考虑。

UPDATE 2015-04-24

更新 2015-04-24

We've considered the addition of a variety of static, named functions such as Predicate.alwaysTrue, Runnable.noop, etc., and we have decided not to add any more in future versions of Java SE.

我们认为除了各种静态的,命名功能,如Predicate.alwaysTrueRunnable.noop等,我们已决定不添加任何更多的在Java SE的未来版本。

Certainly there is some value in something that has a name vs. a written-out lambda, but this value is quite small. We expect that people will learn how to read and write x -> trueand () -> { }and that their usage will become idiomatic. Even the value of Function.identity()over x -> xis questionable.

当然,有名称的东西与写出的 lambda 相比有一些价值,但这个价值非常小。我们期望人们将学习如何阅读和写作x -> true() -> { }并且他们的用法将成为惯用语。甚至Function.identity()over的价值x -> x也值得怀疑。

There is a tiny performance advantage to reusing an existing function instead of evaluating a written-out lambda, but we expect the usage of these kinds of functions to be so small that such an advantage would be negligible, certainly not worth the API bloat.

重用现有函数而不是评估写出的 lambda 有微小的性能优势,但我们预计这些类型的函数的使用量很小,以至于这种优势可以忽略不计,当然不值得 API 膨胀。

Holger also mentioned in comments the possibility of optimizing composed functions such as Predicate.orand such. This was also considered (JDK-8067971) but was deemed somewhat fragile and error-prone, and occurring infrequently enough that it wasn't worth the effort to implement.

Holger 还在评论中提到了优化组合函数的可能性,Predicate.or诸如此类。这也被考虑过(JDK-8067971),但被认为有些脆弱且容易出错,而且发生的频率很低,因此不值得努力实现。

See also this Lambda FAQentry.

另请参阅此Lambda 常见问题条目。

回答by boriselec

Without guava

没有番石榴

Boolean.TRUE::booleanValue