这个类是这样的:
public static void main(String[] args) {
ArrayList<UserEntity> userEntities = new ArrayList<>();
userEntities.add(new UserEntity("mayikt", 20));
userEntities.add(new UserEntity("zhangsan", 35));
userEntities.add(new UserEntity("xiaowei", 16));
userEntities.add(new UserEntity("mayikt_list", 109));
userEntities.add(new UserEntity("mayikt", 32));
userEntities.add(new UserEntity("mayikt", 29));
userEntities.add(new UserEntity("mayikt", 40));
Stream<UserEntity> stream = userEntities.stream();
// Lambda表达式
stream.sorted((o1, o2) -> o2.getAge() - o1.getAge()) // age 降序排列
.filter(userEntity -> "mayikt".equals(userEntity.getUserName())) // 找到userName为 mayikt 的数据
.limit(2)
.forEach(userEntity -> System.out.println(userEntity));
// 匿名内部类
// stream.sorted(new Comparator<UserEntity>() {
// @Override
// public int compare(UserEntity o1, UserEntity o2) {
// return o2.getAge() - o1.getAge();
// }
// }).filter(new Predicate<UserEntity>() {
// @Override
// public boolean test(UserEntity userEntity) {
// return "mayikt".equals(userEntity.getUserName());
// }
// }).limit(2).forEach(userEntity -> System.out.println(userEntity));
}