/** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t);
public static void main(String[] args) { // Predicate<Integer> predicate = p -> { // return p > 100; // }; Predicate<Integer> predicate = p -> p > 100; System.out.println(predicate.test(50));//false System.out.println(predicate.test(150));//true } }
过滤出偶数:Predicate作为判断逻辑
public static void main(String[] args) { List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); new PredicateTest().findByPredicate(list,p-> p % 2 == 0).forEach(i-> System.out.println(i)); } public List<Integer> findByPredicate(List<Integer> list,Predicate<Integer> predicate){ return list.stream().filter(predicate).collect(Collectors.toList()); }