-
Notifications
You must be signed in to change notification settings - Fork 88
14.4 #229
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?
14.4 #229
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 |
|---|---|---|
|
|
@@ -26,38 +26,28 @@ public String getName() { | |
|
|
||
| // 过滤ID为偶数的用户 | ||
| public static List<User> filterUsersWithEvenId(List<User> users) { | ||
| List<User> results = new ArrayList<>(); | ||
| for (User user : users) { | ||
| if (user.id % 2 == 0) { | ||
| results.add(user); | ||
| } | ||
| } | ||
| return results; | ||
| return filter(users,u->u.getId()%2==0); | ||
| } | ||
|
|
||
| // 过滤姓张的用户 | ||
| 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 results; | ||
| return filter(users,u->u.name.startsWith("张")); | ||
|
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.
|
||
| } | ||
|
|
||
| // 过滤姓王的用户 | ||
| 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 results; | ||
| return filter(users,u->u.name.startsWith("王")); | ||
|
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.
|
||
| } | ||
| // 你可以发现,在上面三个函数中包含大量的重复代码。 | ||
| // 请尝试通过Predicate接口将上述代码抽取成一个公用的过滤器函数 | ||
| // 并简化上面三个函数 | ||
| public static List<User> filter(List<User> users, Predicate<User> predicate) {} | ||
| public static List<User> filter(List<User> users, Predicate<User> predicate) { | ||
| List<User> resultat=new ArrayList<>(); | ||
|
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.
|
||
| for(User u: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.
|
||
| if(predicate.test(u)){ | ||
|
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.
|
||
| resultat.add(u); | ||
| } | ||
| } | ||
| return resultat; | ||
| } | ||
| } | ||
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.
',' 后应有空格。'->' 后应有空格。'->' 前应有空格。'%' 后应有空格。'%' 前应有空格。'==' 后应有空格。'==' 前应有空格。