-
Notifications
You must be signed in to change notification settings - Fork 93
通过Predicate接口将上述代码抽取成一个公用的过滤器函数 #198
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
Open
hahaen
wants to merge
2
commits into
hcsp:master
Choose a base branch
from
hahaen:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,64 @@ public String getName() { | |
return name; | ||
} | ||
|
||
// 过滤ID为偶数的用户 | ||
public static List<User> filterUsersWithEvenId(List<User> users) { | ||
public static List<User> filter(List<User> users, Predicate<User> predicate) { | ||
List<User> 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<User> filterUsersWithEvenId(List<User> users) { | ||
return filter(users, new Predicate<User>() { | ||
@Override | ||
public boolean test(User user) { | ||
return user.id%2==0; | ||
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.
|
||
} | ||
}); | ||
} | ||
|
||
|
||
|
||
// 过滤姓张的用户 | ||
public static List<User> filterZhangUsers(List<User> users) { | ||
List<User> results = new ArrayList<>(); | ||
for (User user : users) { | ||
if (user.name.startsWith("张")) { | ||
results.add(user); | ||
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); | ||
return filter(users, new Predicate<User>() { | ||
@Override | ||
public boolean test(User user) { | ||
return user.name.startsWith("王"); | ||
} | ||
} | ||
return results; | ||
}); | ||
} | ||
|
||
// 你可以发现,在上面三个函数中包含大量的重复代码。 | ||
// 请尝试通过Predicate接口将上述代码抽取成一个公用的过滤器函数 | ||
// 并简化上面三个函数 | ||
public static List<User> filter(List<User> users, Predicate<User> predicate) {} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
',' 后应有空格。
',' 后应有空格。
',' 后应有空格。