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

2024-03-28 Java8之Collectors类

Collectors类常用方法

文章目录

  • Collectors类常用方法
    • 1.toList、toSet、toMap
    • 2.joining、counting、summingInt、minBy
    • 3.groupingBy

1.toList、toSet、toMap

Collector<T, ?, List<T>> toList(); //收集为List集合
Collector<T, ?, Set<T>> toSet(); //Set
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,Function<? super T, ? extends U> valueMapper)//Map
//收集code为新List集合
List<String> strList = list.stream().map(DemoObj::getCode).collect(Collectors.toList());
//收集code为新Set集合
Set<String> collect = list.stream().map(DemoObj::getCode).collect(Collectors.toSet());
//将code为key,amount为值作为map集合
Map<String, Integer> collect = list.stream().collect(Collectors.toMap(DemoObj::getCode, DemoObj::getAmount));

2.joining、counting、summingInt、minBy

//指定字符拼接
Collector<CharSequence, ?, String> joining(CharSequence delimiter); 
//统计 等同于Stream的count()
<T> Collector<T, ?, Long> counting();
//求和,等同于Stream的mapToInt(ToIntFunction<? super T> mapper).sum()
<T> Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper) 
//获取最小值,等同于Stream的min(Comparator<? super T> comparator)
<T> Collector<T, ?, Optional<T>>  minBy(Comparator<? super T> comparator) 
//遍历list集合取将DemoObj对象的code属性用逗号拼接
String str = list.stream().map(DemoObj::getCode).collect(Collectors.joining(","));
Long collect = list.stream().map(DemoObj::getCode).collect(Collectors.counting());
//求和,等同于mapToInt(DemoObj::getAmount).sum()
Integer collect = list.stream().collect(Collectors.summingInt(DemoObj::getAmount));
//获取最小金额,等同于 min(Comparator.comparingInt(DemoObj::getAmount))
Optional<DemoObj> collect = list.stream().collect(Collectors.minBy(Comparator.comparingInt(DemoObj::getAmount)));

3.groupingBy

<T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier);//分组
Map<Integer, List<DemoObj>> collect = list.stream().collect(Collectors.groupingBy(DemoObj::getAmount));//按金额分组

相关文章:

  • MybatisPlus速成
  • Hive查询转换与Hadoop生态系统引擎与优势
  • python---基础(一)
  • 发生播放错误,即将重试 jellyfin
  • 集合框架——Map
  • MySQL索引特性
  • 备考ICA----Istio实验12---配置双向TLS Istio Ingress Gateway实验
  • 【Web自动化】Selenium的使用(一)
  • C# OpenCvSharp 轮廓检测
  • 每天学习一个Linux命令之uniq
  • Python爬虫-懂车帝城市销量榜单
  • BabySQL【2019极客大挑战】
  • 前端小白的学习之路(webpack)
  • python安装删除以及pip的使用
  • 【Node.js从基础到高级运用】十九、Node.js 捕获错误之“未捕获的异常”
  • CSS选择器——伪元素选择器之处理父元素高度及外边距溢出
  • es6
  • iOS 系统授权开发
  • js面向对象
  • Linux CTF 逆向入门
  • oldjun 检测网站的经验
  • 容器化应用: 在阿里云搭建多节点 Openshift 集群
  • 如何编写一个可升级的智能合约
  • 使用权重正则化较少模型过拟合
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • 学习HTTP相关知识笔记
  • 用 Swift 编写面向协议的视图
  • 鱼骨图 - 如何绘制?
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • Semaphore
  • 阿里云ACE认证学习知识点梳理
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (2020)Java后端开发----(面试题和笔试题)
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (剑指Offer)面试题34:丑数
  • ***监测系统的构建(chkrootkit )
  • .Net Core与存储过程(一)
  • .NET 服务 ServiceController
  • .NET(C#) Internals: as a developer, .net framework in my eyes
  • .NET/C# 的字符串暂存池
  • .NET成年了,然后呢?
  • .net分布式压力测试工具(Beetle.DT)
  • ::before和::after 常见的用法
  • @vue/cli脚手架
  • [ C++ ] STL_list 使用及其模拟实现
  • [20170713] 无法访问SQL Server
  • [2019/05/17]解决springboot测试List接口时JSON传参异常
  • [23] GaussianAvatars: Photorealistic Head Avatars with Rigged 3D Gaussians
  • [bzoj1912]异象石(set)
  • [C#]使用PaddleInference图片旋转四种角度检测
  • [C++]打开新世界的大门之C++入门
  • [Java][Android][Process] ProcessBuilder与Runtime差别