Skip to content
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
95 changes: 68 additions & 27 deletions src/main/java/com/github/hcsp/polymorphism/User.java
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;

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.

无用导入 - com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput 。


import java.util.ArrayList;
import java.util.Arrays;

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.

无用导入 - 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 +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 ;

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.

';' 前不应有空格。

}
});
}
/* 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) {

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.

'<' 后不应有空格。
'<' 前不应有空格。
'>' 前不应有空格。

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){

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.

'{' 前应有空格。

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,"王")));
}*/
}