···
public List<User> getUsers(){
User a = new User(1, "a", 21);
User b = new User(2, "b", 22);
User c = new User(3, "c", 23);
User d = new User(4, "d", 24);
User f = new User(5, "f", 25);
List<User> asList = Arrays.asList(a, b, c, d, f);
return asList;
}
/**
* 1,ID必须是偶数的
*/
public void test1(){
List<User> users = this.getUsers();
List<User> collect = users.stream().filter(item -> item.getId() % 2 == 0).collect(Collectors.toList());
System.out.println(JSON.toJSON(collect));
}
/**
* 2,年龄必须大于25岁
*/
public void test2(){
List<User> users = this.getUsers();
List<User> collect = users.stream().filter(item -> item.getAge() > 23 ).collect(Collectors.toList());
System.out.println(JSON.toJSON(collect));
}
/**
* 3,用户名转大写
*/
public void test3(){
List<User> users = this.getUsers();
users.forEach(item->item.setName( item.getName().toUpperCase()));
System.out.println(JSON.toJSONString(users));
}
/**
* 4,用户名字母倒叙
*/
public void tes4(){
List<User> users = this.getUsers();
users.stream().sorted(Comparator.comparing(User::getName));
}
/**
* 5,只输出一个用户
*/
public void test5(){
List<User> users = this.getUsers();
Stream<User> limit = users.stream().limit(1);
}
···