java8之stream API 常用总结

List<User> list=Arrays.asList(user,user2,user3);

1.forEach() 遍历

eg:  list.stream().forEach(user->System.out.println(user)); // stream ,lambda

2.filter() 过滤

eg: list.stream().filter((user->user.getAge>20).forEach(user->System.out.println(user));

3.sorted() 排序

eg: list.stream().sorted(Comparator.comparing(User::getCreationTime)).forEach(user -> System.out.println(user));

4.limit() 截取
eg: list.stream().limit(3).forEach(user->System.out.println(user));

5.distinct() 去重

eg: list.stream().distinct().forEach(user->System.out.println(user))

6.map() 映射
eg: list.stream().map(user->user.getName).collect(Collectors.toList());

7.findFirst() 查找第一个
eg: list.stream().findFirst().get();

补充:Collection中新增加的方法removeIf

 list.removeAll(user);

猜你喜欢

转载自blog.csdn.net/lsy_know/article/details/86231484
今日推荐