-
Notifications
You must be signed in to change notification settings - Fork 87
使用接口抽取过滤器函数 #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
使用接口抽取过滤器函数 #204
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,21 @@ | ||
| package com.github.hcsp.polymorphism; | ||
|
|
||
| import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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 +31,74 @@ public String getName() { | |
| return name; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /* public static class 偶数 implements 判断条件 { | ||
| @Override | ||
| public boolean 判断用户满足条件(User user) { | ||
| return user.id % 2 == 0; | ||
| } | ||
| }*/ | ||
| /* public static class 姓张 implements 判断条件 { | ||
| @Override | ||
| public boolean 判断用户满足条件(User user) { | ||
| return user.name.startsWith("张"); | ||
| } | ||
| }*/ | ||
|
|
||
| // 过滤ID为偶数的用户 | ||
| public static List<User> filterUsersWithEvenId(List<User> users) { | ||
| List<User> results = new ArrayList<>(); | ||
| for (User user : users) { | ||
| return filter(users, new Predicate<User>() { | ||
| @Override | ||
| public boolean test(User user) { | ||
| return user.id % 2 == 0 ; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| }); | ||
| } | ||
| /* List<User> results = new ArrayList<>(); //结果接收器 | ||
| for (User user : users) { // for遍历查找 | ||
| if (user.id % 2 == 0) { | ||
| results.add(user); | ||
| results.add(user); //符合条件 结果+到列表中去 | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
| return results; //返回新的列表 | ||
| }*/ | ||
|
|
||
| // 过滤姓张的用户 | ||
| public static List<User> filterZhangUsers(List<User> users) { | ||
| List<User> results = new ArrayList<>(); | ||
| for (User user : users) { | ||
| if (user.name.startsWith("张")) { | ||
| results.add(user); | ||
| // 过滤姓张的用户 | ||
| public static List<User> filterZhangUsers (List<User> users) { | ||
| return filter(users, new Predicate<User>() { | ||
| @Override | ||
| public boolean test(User user) { | ||
| return user.name.startsWith("张"); | ||
| } | ||
| } | ||
| return results; | ||
| }); | ||
| } | ||
|
|
||
| // 过滤姓王的用户 | ||
| public static List<User> filterWangUsers(List<User> users) { | ||
| List<User> results = new ArrayList<>(); | ||
| for (User user : users) { | ||
| if (user.name.startsWith("王")) { | ||
| results.add(user); | ||
| // 过滤姓王的用户 | ||
| public static List<User> filterWangUsers (List < User > users) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return filter(users, new Predicate<User>() { | ||
| @Override | ||
| public boolean test(User user) { | ||
| return user.name.startsWith("王"); | ||
| } | ||
| }); | ||
| } | ||
| // 你可以发现,在上面三个函数中包含大量的重复代码。 | ||
| // 请尝试通过Predicate接口将上述代码抽取成一个公用的过滤器函数 | ||
| // 并简化上面三个函数 | ||
| public static List<User> filter(List<User> users, Predicate<User> predicate){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| List<User> results = new ArrayList<>(); | ||
| for (User user : users) { | ||
| if (predicate.test(user)) { | ||
| results.add(user); | ||
| // System.out.println(user.id); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
| return results; | ||
| } | ||
| // 你可以发现,在上面三个函数中包含大量的重复代码。 | ||
| // 请尝试通过Predicate接口将上述代码抽取成一个公用的过滤器函数 | ||
| // 并简化上面三个函数 | ||
| public static List<User> filter(List<User> users, Predicate<User> predicate) {} | ||
|
|
||
| /* public static void main(String[] args) { | ||
| filterUsersWithEvenId(Arrays.asList(new User(1,"李"),new User(2,"王"))); | ||
| }*/ | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
无用导入 - com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput 。