From 90e13cc1f28a7363a48ec969725b5b25d466127e Mon Sep 17 00:00:00 2001 From: zeekling Date: Sun, 4 Jul 2021 16:38:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=87=BD=E6=95=B0=E5=BC=8F?= =?UTF-8?q?=E7=BC=96=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zeekling/function/FunctionTest.java | 37 +++++++++++++++++++ .../java/com/zeekling/function/Predicate.java | 18 +++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/main/java/com/zeekling/function/FunctionTest.java create mode 100644 src/main/java/com/zeekling/function/Predicate.java diff --git a/src/main/java/com/zeekling/function/FunctionTest.java b/src/main/java/com/zeekling/function/FunctionTest.java new file mode 100644 index 0000000..58ac218 --- /dev/null +++ b/src/main/java/com/zeekling/function/FunctionTest.java @@ -0,0 +1,37 @@ +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())); + + } + +} diff --git a/src/main/java/com/zeekling/function/Predicate.java b/src/main/java/com/zeekling/function/Predicate.java new file mode 100644 index 0000000..ce716fe --- /dev/null +++ b/src/main/java/com/zeekling/function/Predicate.java @@ -0,0 +1,18 @@ +package com.zeekling.function; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author zeekling + * @apiNote 函数式编程测试 + * @since 2021-07-04 + */ +@FunctionalInterface +public interface Predicate { + + boolean test(T t); + + + +}