Java中的谓词 [英] Predicate in Java
问题描述
我正在浏览Java中使用 Predicate
的代码。我从未使用 Predicate
。有人可以指导我对 Predicate
的任何教程或概念性解释及其在Java中的实现吗?
I am going through the code which uses Predicate
in Java. I have never used Predicate
. Can someone guide me to any tutorial or conceptual explanation of Predicate
and its implementation in Java?
推荐答案
我假设你在谈论 com.google.common.base.Predicate< T>
。
I'm assuming you're talking about com.google.common.base.Predicate<T>
from Guava.
来自API:
确定
true
或false
给定输入的值。例如,RegexPredicate
可能实现Predicate< String>
,并对与其给定正则表达式匹配的任何字符串返回true 。
Determines a
true
orfalse
value for a given input. For example, aRegexPredicate
might implementPredicate<String>
, and return true for any string that matches its given regular expression.
这实际上是布尔
测试的OOP抽象。
This is essentially an OOP abstraction for a boolean
test.
例如,您可能有这样的帮助方法:
For example, you may have a helper method like this:
static boolean isEven(int num) {
return (num % 2) == 0; // simple
}
现在,给定 List< Integer> ;
,你只能处理这样的偶数:
Now, given a List<Integer>
, you can process only the even numbers like this:
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
for (int number : numbers) {
if (isEven(number)) {
process(number);
}
}
使用谓词
,如果
测试被抽象出来作为一种类型。这允许它与API的其余部分进行互操作,例如 Iterables
,它有许多实用方法,需要 Predicate
。
With Predicate
, the if
test is abstracted out as a type. This allows it to interoperate with the rest of the API, such as Iterables
, which have many utility methods that takes Predicate
.
因此,你现在可以这样写:
Thus, you can now write something like this:
Predicate<Integer> isEven = new Predicate<Integer>() {
@Override public boolean apply(Integer number) {
return (number % 2) == 0;
}
};
Iterable<Integer> evenNumbers = Iterables.filter(numbers, isEven);
for (int number : evenNumbers) {
process(number);
}
请注意,现在for-each循环更简单,没有 if
test。我们通过定义 Iterable< Integer>来达到更高的缩减水平。 evenNumber
,按过滤器
-ing使用谓词
。
Note that now the for-each loop is much simpler without the if
test. We've reached a higher level of abtraction by defining Iterable<Integer> evenNumbers
, by filter
-ing using a Predicate
.
-
Iterables.filter
- 返回满足谓词的元素。
谓词
允许Iterables.filter
作为什么叫做高阶函数。就其本身而言,这提供了许多优点。取列表<整数>数字
上面的例子。假设我们想测试所有数字是否为正数。我们可以写这样的东西:Predicate
allowsIterables.filter
to serve as what is called a higher-order function. On its own, this offers many advantages. Take theList<Integer> numbers
example above. Suppose we want to test if all numbers are positive. We can write something like this:static boolean isAllPositive(Iterable<Integer> numbers) { for (Integer number : numbers) { if (number < 0) { return false; } } return true; } //... if (isAllPositive(numbers)) { System.out.println("Yep!"); }
使用
谓词
,并与其他库互操作,我们可以写这个:With a
Predicate
, and interoperating with the rest of the libraries, we can instead write this:Predicate<Integer> isPositive = new Predicate<Integer>() { @Override public boolean apply(Integer number) { return number > 0; } }; //... if (Iterables.all(numbers, isPositive)) { System.out.println("Yep!"); }
希望您现在可以看到更高抽象的值,例如filter all给定谓词的元素,检查所有元素是否满足给定的谓词等等,以获得更好的代码。
Hopefully you can now see the value in higher abstractions for routines like "filter all elements by the given predicate", "check if all elements satisfy the given predicate", etc make for better code.
不幸的是,Java没有一流的方法:你不能将方法传递给
Iterables.filter
和Iterables.all
。当然,您可以在Java中传递对象。因此,定义了谓词
类型,并传递实现此接口的对象。Unfortunately Java doesn't have first-class methods: you can't pass methods around to
Iterables.filter
andIterables.all
. You can, of course, pass around objects in Java. Thus, thePredicate
type is defined, and you pass objects implementing this interface instead.- Wikipedia/Higher-order function
- Wikipedia/Filter (higher-order function)
这篇关于Java中的谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!