一、代码测试案例
package demo;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import entity.User;
public class Test01 {
public static void main(String[] args) {
List<User> list = new ArrayList<>();
User user1 = new User("张三",25,"男");
User user2 = new User("小李",38,"男");
User user3 = new User("小红",40,"女");
list.add(user1);
list.add(user2);
list.add(user3);
System.out.println("集合的所有信息: " + list);
System.out.println("");
List<User> collect = list.stream()
.filter(user -> !"张三".equals(user.getName()))
.collect(Collectors.toList());
System.out.println("单一条件过滤后:" + collect);
System.out.println("");
List<User> collect2 = list.stream()
.filter(user -> user.getAge() >= 40 && "女".equals(user.getSex()))
.collect(Collectors.toList());
System.out.println("多条件过滤后:" + collect2);
}
}
二、测试效果图如下: