单一条件过滤
/** * 测试filter */ @Test public void testFilter() { List<User> user = new ArrayList<>(); user.add(new User(1L, 18, "小明")); user.add(new User(2L, 20, "小王")); user.add(new User(3L, 28, "小刚")); user.add(new User(4l, 25, "小丽")); user.add(new User(5L, 18, "小明")); List<User> collect1 = user.stream().filter(e -> e.getAge() > 20).collect(Collectors.toList()); System.out.println(JSON.toJSONString(collect1)); }
多条件过滤
/** * 测试filter */ @Test public void testFilter() { List<User> user = new ArrayList<>(); user.add(new User(1L, 18, "小明")); user.add(new User(2L, 20, "小王")); user.add(new User(3L, 28, "小刚")); user.add(new User(4l, 25, "小丽")); user.add(new User(5L, 18, "小明")); List<User> collect = user.stream().filter(e -> e.getAge() > 20 && e.getId() > 3).collect(Collectors.toList()); System.out.println(JSON.toJSONString(collect)); }