新建一个实体类
public class User {
private Integer ID;
private String name;
private int age;
private String birthday;
public User(Integer ID, String name, int age, String birthday) {
this.ID = ID;
this.name = name;
this.age = age;
this.birthday = birthday;
}
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"ID=" + ID +
", name='" + name + '\'' +
", age=" + age +
", birthday='" + birthday + '\'' +
'}';
}
}
List过滤的三种方式:
- 通过java8中filter过滤器进行过滤
- 通过For循环遍历过滤
- 通过ForEach遍历过滤
public class FilteringList {
/**
* 通过java8中filter过滤器进行过滤
* @param userList
* @return
*/
public List<User> filterByStream(List<User> userList){
return userList.stream()
.filter(user -> Objects.nonNull(user))
.filter(user -> user.getAge()>=12)
.collect(Collectors.toList());
}
/**
* 通过For循环遍历过滤
* @param userList
* @return
*/
public List<User> filterByFor(List<User> userList){
List<User> newUserList = new ArrayList<>();
for (User user : userList) {
if(user != null && user.getAge()>=12){
newUserList.add(user);
}
}
return newUserList;
}
/**
* 通过ForEach遍历过滤
* @param userList
* @return
*/
public List<User> filterByForEach(List<User> userList){
List<User> newUserList = new ArrayList<>();
userList.forEach(user -> {
if(user != null && user.getAge()>=12){
newUserList.add(user);
}
});
return newUserList;
}
}
测试
public class TestFilteringList {
public static List<User> userList;
static {
User zhangSan = new User(1,"张三",10,"2001-01-02");
User liSi = new User(2,"李四",11,"2001-01-02");
User wangWu = new User(3,"王五",12,"2003-01-02");
User zhaoLiu = new User(4,"赵六",18,"2003-01-02");
userList = Arrays.asList(zhangSan,liSi,wangWu,zhaoLiu);
}
@Test
public void testFilterByStream(){
FilteringList filteringList = new FilteringList();
System.out.println("过滤前:" + userList);
List<User> resUserList = filteringList.filterByStream(userList);
System.out.println("过滤后:" + resUserList);
// 过滤前:
// [User{ID=1, name='张三', age=10, birthday='2001-01-02'},
// User{ID=2, name='李四', age=11, birthday='2001-01-02'},
// User{ID=3, name='王五', age=12, birthday='2003-01-02'},
// User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]
// 过滤后:
// [User{ID=3, name='王五', age=12, birthday='2003-01-02'},
// User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]
}
@Test
public void testFilterByFor(){
FilteringList filteringList = new FilteringList();
System.out.println("过滤前:" + userList);
List<User> resUserList = filteringList.filterByFor(userList);
System.out.println("过滤后:" + resUserList);
// 过滤前:
// [User{ID=1, name='张三', age=10, birthday='2001-01-02'},
// User{ID=2, name='李四', age=11, birthday='2001-01-02'},
// User{ID=3, name='王五', age=12, birthday='2003-01-02'},
// User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]
// 过滤后:
// [User{ID=3, name='王五', age=12, birthday='2003-01-02'},
// User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]
}
@Test
public void testFilterByForEach(){
FilteringList filteringList = new FilteringList();
System.out.println("过滤前:" + userList);
List<User> resUserList = filteringList.filterByForEach(userList);
System.out.println("过滤后:" + resUserList);
// 过滤前:
// [User{ID=1, name='张三', age=10, birthday='2001-01-02'},
// User{ID=2, name='李四', age=11, birthday='2001-01-02'},
// User{ID=3, name='王五', age=12, birthday='2003-01-02'},
// User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]
// 过滤后:
// [User{ID=3, name='王五', age=12, birthday='2003-01-02'},
// User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]
}
}