当前位置: 首页 > news >正文

java list stream 使用

1、实现List对象集合的简单去重(distinct()) ​

List<User> list = list.stream().distinct().collect(Collectors.toList());

 ​2、实现List集合的根据属性(name)去重

list = list.stream().filter(o -> o.getName() != null).collect( Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName()))),ArrayList<Aoo>::new));

3、实现List对象集合的简单过滤(过滤为 null 的对象)

List<User> list = list.stream().filter(item -> item != null).collect(Collectors.toList());

4、实现List对象集合中获取其中某一属性的List集合

List<String> collect = apples.stream().map(User::getName).collect(Collectors.toList());

5、 现List对象集合中根据对象的某一属性进行分组

Map<String, List<User>> userMap = list.stream().collect(Collectors.groupingBy(User :: getName));

6、实现List对象集合中求和、最大、最小、平均的统计

Long sum = list.stream().mapToLong(User::getAge).sum(); //和
Double max = list.stream().mapToDouble(User::getAge).max(); //最大
Integer = list.stream().mapToInt(User::getAge).min(); //最小
OptionalDouble average = list.stream().mapToDouble(User::getAge).average(); //平均值

7、实现List对象集合的分页

List<User> resultList = list.stream().skip(pageSize * (currentPage - 1)).limit(pageSize).collect(Collectors.toList());

http://www.lryc.cn/news/95554.html

相关文章:

  • 两个Ubuntu电脑用SSH远程连接
  • 讲解 @ServletComponentScan注解
  • 20款奔驰S350商务型加装原厂前排座椅通风系统,夏天必备的功能
  • Rust vs Go:常用语法对比(十一)
  • Spring MVC拦截器和跨域请求
  • C++初阶--C++入门
  • Matlab实现PID控制仿真(附上30个完整仿真源码+数据)
  • 微信小程序:文件下载
  • QString和QByteArray的区别
  • Vue3 Vite electron 开发桌面程序
  • 【Nodejs】Express模板使用
  • 【iOS】App仿写--管理系统
  • JS实现队列的数据结构
  • title: 用 LangChain 构建基于资料库的问答机器人(四):通过代理使用外部工具
  • 使用 CSS 自定义属性
  • Unity 性能优化一:性能标准、常用工具
  • 【http长连接+池化】
  • opencv-20 深入理解HSV 色彩空间(通过指定,标记颜色等来拓展ROI区域)
  • python调用arcgis功能一例
  • Spring MVC 是什么?
  • Rust操作MySQL
  • JAVA面试总结-Redis篇章(二)——缓存击穿
  • Spring相关知识点
  • Nginx专题--反向代理(未完成)
  • 什么是搜索引擎?2023 年搜索引擎如何运作?
  • Spring系列一:spring的安装与使用
  • Ubuntu--科研工具系列
  • 【压测指南|压力测试核心性能指标及行业标准】
  • spark-submit --files
  • 应该选云服务器还是物理服务器