JAVA8中的Predicate

JAVA8中引入了大量的函数式编程关键字。
Predicate就是其中之一。

/**
 * Represents a predicate (boolean-valued function) of one argument.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #test(Object)}.
 *
 * @param <T> the type of the input to the predicate
 *
 * @since 1.8
 */

给定一个参数,返回真假。

以Arraylist的removeIf为例。
要求:移除list中所有的偶数
实现:

Predicate<Integer> isEven = x -> (x % 2) == 0;
list.removeIf(isEven);

简直简洁。

猜你喜欢

转载自blog.csdn.net/weixin_42498646/article/details/87716823