From 26385313e2e4fabe70c06b2aa5e3d8a1b8df1a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cidpeng=E2=80=9D?= <630606938@qq.com> Date: Tue, 31 Aug 2021 13:54:00 +0800 Subject: [PATCH 1/2] return filter(users, new Predicate() { --- .../com/github/hcsp/polymorphism/User.java | 67 ++++++++++++++----- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/github/hcsp/polymorphism/User.java b/src/main/java/com/github/hcsp/polymorphism/User.java index 51c95dc..0878c64 100644 --- a/src/main/java/com/github/hcsp/polymorphism/User.java +++ b/src/main/java/com/github/hcsp/polymorphism/User.java @@ -1,14 +1,19 @@ package com.github.hcsp.polymorphism; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.function.Predicate; public class User { - /** 用户ID,数据库主键,全局唯一 */ + /** + * 用户ID,数据库主键,全局唯一 + */ private final Integer id; - /** 用户名 */ + /** + * 用户名 + */ private final String name; public User(Integer id, String name) { @@ -24,40 +29,66 @@ public String getName() { return name; } - // 过滤ID为偶数的用户 - public static List filterUsersWithEvenId(List users) { + public static List filter(List users, Predicate predicate) { List results = new ArrayList<>(); for (User user : users) { - if (user.id % 2 == 0) { + if (predicate.test(user)) { results.add(user); } } return results; } +// private interface 判断条件是否成立 { +// boolean 这个用户是否满足条件(User user); +// } + +// private static class 判断id是不是偶数 implements 判断条件是否成立{ +// @Override +// public boolean 这个用户是否满足条件(User user) { +// return user.id%2==0; +// } +// } + + public static void main(String[] args) { + filterUsersWithEvenId(Arrays.asList(new User(1,"a"),new User(2,"b"))); + } + // 过滤ID为偶数的用户 + public static List filterUsersWithEvenId(List users) { + return filter(users, new Predicate() { + @Override + public boolean test(User user) { + return user.id%2==0; + } + }); + } + + + // 过滤姓张的用户 public static List filterZhangUsers(List users) { - List results = new ArrayList<>(); - for (User user : users) { - if (user.name.startsWith("张")) { - results.add(user); + return filter(users, new Predicate() { + @Override + public boolean test(User user) { + return user.name.startsWith("张"); } - } - return results; + }); } // 过滤姓王的用户 public static List filterWangUsers(List users) { - List results = new ArrayList<>(); - for (User user : users) { - if (user.name.startsWith("王")) { - results.add(user); + return filter(users, new Predicate() { + @Override + public boolean test(User user) { + return user.name.startsWith("王"); } - } - return results; + }); } + // 你可以发现,在上面三个函数中包含大量的重复代码。 // 请尝试通过Predicate接口将上述代码抽取成一个公用的过滤器函数 // 并简化上面三个函数 - public static List filter(List users, Predicate predicate) {} +// public static List filter(List users, Predicate predicate) { +// +// } } From 0d610466352dfd6650538722f750fb2cb0f8cbe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cidpeng=E2=80=9D?= <630606938@qq.com> Date: Tue, 31 Aug 2021 13:56:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E9=80=9A=E8=BF=87Predicate=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=B0=86=E4=B8=8A=E8=BF=B0=E4=BB=A3=E7=A0=81=E6=8A=BD?= =?UTF-8?q?=E5=8F=96=E6=88=90=E4=B8=80=E4=B8=AA=E5=85=AC=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E5=99=A8=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/github/hcsp/polymorphism/User.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/github/hcsp/polymorphism/User.java b/src/main/java/com/github/hcsp/polymorphism/User.java index 0878c64..6bdb24a 100644 --- a/src/main/java/com/github/hcsp/polymorphism/User.java +++ b/src/main/java/com/github/hcsp/polymorphism/User.java @@ -88,7 +88,5 @@ public boolean test(User user) { // 你可以发现,在上面三个函数中包含大量的重复代码。 // 请尝试通过Predicate接口将上述代码抽取成一个公用的过滤器函数 // 并简化上面三个函数 -// public static List filter(List users, Predicate predicate) { -// -// } + }