每一年都奔走在自己热爱里

 
没有人是一座孤岛,总有谁爱着你

【集合常用的方法】

1.List判空二种方法:

1.list != null && list.size() != 0
2.CollectionUtils.isEmpty(list);

2. 获取list某个对象属性值的和

        //第一种 计数
        long sum1= list.stream().mapToLong(Entity::getId).sum();
        list.stream().mapToInt/mapDouble//数据类型不同
        //第二种 技术
        LongSummaryStatistics statistics= list.stream().mapToLong(entity -> entity .getId())).summaryStatistics();
        long sum2 = statistics.getSum();
        //其他常用的方法
        statistics.getMax();//最大值
        statistics.getMin();最小值
        statistics.getAverage();平均值

3. 根据list中对象某个属性值分组

//分组
Map<String, List<Entity>> groupMap=list.stream().collect(Collectors.groupingBy(Entity::getId));
//计数
Map<String, Long> countMap=list.stream().collect(Collectors.groupingBy(Entity::getId,Collectors.counting()));
//分组并根据另一个属性计数
Map<String, Long> countMap=list.stream().collect(Collectors.groupingBy(Entity::getId,Collectors.summingInt(Entity::getPrice)));

4. 将list转换为map

//第一种
 list.stream().collect(Collectors.toMap(Entity::getId, Entity::getName));
//第二种
 list.stream().collect(Collectors.toMap(t -> t.getId()+"#"+t.getPrice(),p -> p.getName())
posted @   与乐i  阅读(44)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
Live2D
欢迎阅读『【集合常用的方法】』
// 生成目录索引列表 //侧边栏目录索引
点击右上角即可分享
微信分享提示