java8基础操作
类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
}
测试
public static void main(String[] args) {
//年龄升序
List<User> userList=new ArrayList<>();
userList.add(new User("xhs",22));
userList.add(new User("lksdfjls",855));
userList.add(new User("kk",88));
// List<User> ascByAgeList = userList.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
// System.out.println("年龄上升排序");
// System.out.println(ascByAgeList);
// List<User> descList = userList.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());
// System.out.println("年龄降序");
// System.out.println(descList);
//名称分组
// Map<String, List<User>> nameMap = userList.stream().collect(Collectors.groupingBy(User::getName));
// System.out.println(nameMap);
//过滤年龄大于25
List<User> users = userList.stream().filter(user -> user.getAge() > 25).collect(Collectors.toList());
System.out.println(users);
}