lambda表达式进行分页和排序

分页:

List<UserTrainSchedule> userTrainSchedules = new ArrayList<>();
List<UserTrainSchedule> collect = userTrainSchedules.stream().skip(pageSize * (pageNo - 1)).limit(pageSize).collect(Collectors.toList());

lambda表达式排序:

List<User> list1 = new ArrayList<User>();
User user = new User("张三", "15", "男");
User user1 = new User("李四", "10", "男");
list1.add(user);
list1.add(user1);
//1、年龄升序
list1.sort((a,b) -> a.getAge().compareTo(b.getAge()));
//2、姓名降序排列
list1.sort(Comparator.comparing(User::getName).reversed());
//等价于 2
list1.sort(Comparator.comparing(a->((User)a).getAge()).reversed());
//3、先按性别排,如果年龄相同,再按年龄排序
list1.sort(Comparator.comparing(User::getSex).reversed().thenComparing(User::getAge));

你可能感兴趣的:(java8新特性)