Skip to content
Open

14.4 #229

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions src/main/java/com/github/hcsp/polymorphism/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 results;
return filter(users,u->u.name.startsWith("张"));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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("王"));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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<>();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'=' 后应有空格。
'=' 前应有空格。

for(User u:users){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'for' 后应有空格。
'{' 前应有空格。

if(predicate.test(u)){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'if' 后应有空格。
'{' 前应有空格。

resultat.add(u);
}
}
return resultat;
}
}