package com.zeekling.function; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @author zeekling * @apiNote 函数式编程 * @since 2021-07-04 */ public class FunctionTest { public static List filter(List list, Predicate p) { List results = new ArrayList<>(); for(T s: list){ if(p.test(s)){ results.add(s); } } return results; } public static void main(String[] args) { List listOfStrings = new ArrayList(){{ add(""); add("1"); add("2"); }}; Predicate nonEmptyStringPredicate = (String s) -> !s.isEmpty(); List nonEmpty = filter(listOfStrings, nonEmptyStringPredicate); System.out.println(Arrays.toString(nonEmpty.toArray())); } }