断言型接口: T:入参类型,返回值类型 boolean
调用方法: boolean test(T t);
用途: 接收一个参数,用于判断是否满足一定的条件,过滤数据
- /**
- * @param
the type of the input to the predicate - */
- @FunctionalInterface
- public interface Predicate
{ -
- boolean test(T t);
- }
- public static List
filter(List list, Predicate predicate) { - List
results = new ArrayList<>(); - list.forEach(s -> {
- if (predicate.test(s)) {
- results.add(s);
- }
- });
- return results;
- }
- public static void main(String[] args) {
- List
list = Arrays.asList("alis", "bron", "caiwen", "duo", "fanc", "abc"); - List
results = filter(list, obj -> obj.startsWith("a")); - log.info(list.toString());
- log.info(results.toString());
-
- }
