ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 2.2.1 Predicate Predicate接收一个T行为,返回boolean类型的值,以下是其源码: ```java @FunctionalInterface public interface Predicate<T> { boolean test(T t); } ``` 下面的例子展示了Predicate的用法: ```java import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; public class PredicateTest { public static void main(String[] args) { List<Integer> numberList = new ArrayList<>(); Predicate<Integer> oddPredicate = i -> (i != null && i % 2 == 1); List<Integer> oddNumberList = filter(numberList, oddPredicate); } static <T> List<T> filter(List<T> list, Predicate<T> action) { List<T> result = new ArrayList<>(); for (T item : list) { if (action.test(item)) { result.add(item); } } return result; } } ```