java谓词,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?

解决方案

I'm assuming you're talking about com.google.common.base.Predicate from Guava.

From the API:

Determines a true or false value for a given input. For example, a RegexPredicate might implement Predicate, and return true for any string that matches its given regular expression.

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

}

Now, given a List, you can process only the even numbers like this:

List numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);

for (int number : numbers) {

if (isEven(number)) {

process(number);

}

}

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 isEven = new Predicate() {

@Override public boolean apply(Integer number) {

return (number % 2) == 0;

}

};

Iterable evenNumbers = Iterables.filter(numbers, isEven);

for (int number : evenNumbers) {

process(number);

}

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 evenNumbers, by filter-ing using a Predicate.

API links

Iterables.filter

Returns the elements that satisfy a predicate.

On higher-order function

Predicate allows Iterables.filter to serve as what is called a higher-order function. On its own, this offers many advantages. Take the List numbers example above. Suppose we want to test if all numbers are positive. We can write something like this:

static boolean isAllPositive(Iterable 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 isPositive = new Predicate() {

@Override public boolean apply(Integer number) {

return number > 0;

}

};

//...

if (Iterables.all(numbers, isPositive)) {

System.out.println("Yep!");

}

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.

Unfortunately Java doesn't have first-class methods: you can't pass methods around to Iterables.filter and Iterables.all. You can, of course, pass around objects in Java. Thus, the Predicate type is defined, and you pass objects implementing this interface instead.

See also

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值