Skip to content

通过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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
65 changes: 47 additions & 18 deletions src/main/java/com/github/hcsp/polymorphism/User.java
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) {
Expand All @@ -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")));
Copy link
Contributor

Choose a reason for hiding this comment

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

',' 后应有空格。
',' 后应有空格。
',' 后应有空格。

}
// 过滤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;
Copy link
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 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) {}

}