拉姆达表达式实现对集合中的对象中的某个属性求和以及查询某个特定属性以及对特定属性求和以及针对集合根据某个属性以key-value形式分组

本文主要是介绍拉姆达表达式实现对集合中的对象中的某个属性求和以及查询某个特定属性以及对特定属性求和以及针对集合根据某个属性以key-value形式分组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

废话不多说 直接上代码 求和

List<UserEntity> userEntities = new ArrayList<UserEntity>();userEntities.add(new UserEntity(20L, "张三",1,1L));userEntities.add(new UserEntity(20L, "张三",1,1L));userEntities.add(new UserEntity(22L, "李四",2,2L));userEntities.add(new UserEntity(10L, "王五",3,3L));//        Map<String, List<UserEntity>> user = userEntities.stream().collect
//                (Collectors.groupingBy(UserEntity::getAge));Long collect1 = userEntities.stream().collect(Collectors.summingLong(UserEntity::getAge));Long collect2 = userEntities.stream().collect(Collectors.summingLong(UserEntity::getSex));Long collect3 = userEntities.stream().collect(Collectors.summingLong(UserEntity::getUserId));System.out.println(collect1+"000");System.out.println(collect2+"+++");System.out.println(collect3+"---");

运行结果

7000
7+++
72---

代码 查询特定属性

 List<UserEntity> userEntities = new ArrayList<UserEntity>();userEntities.add(new UserEntity(20L, "张三",1,1L));userEntities.add(new UserEntity(20L, "张三",1,1L));userEntities.add(new UserEntity(22L, "李四",2,2L));userEntities.add(new UserEntity(10L, "王五",3,3L));//        Map<String, List<UserEntity>> user = userEntities.stream().collect
//                (Collectors.groupingBy(UserEntity::getAge));List<Long> collect1 = userEntities.stream().map(UserEntity::getAge).collect(Collectors.toList());List<Long> collect2 = userEntities.stream().map(UserEntity::getUserId).collect(Collectors.toList());List<Integer> collect3 = userEntities.stream().map(UserEntity::getSex).collect(Collectors.toList());System.out.println(collect1+" ===");System.out.println(collect2+" ---");System.out.println(collect3+" 000");double sum = userEntities.stream().mapToDouble(UserEntity::getUserId).sum();System.out.println(sum+" sum");

运行结果

[1, 1, 2, 3] ===
[20, 20, 22, 10] ---
[1, 1, 2, 3] 000
72.0 sum

代码 分组

List<UserEntity> userEntities = new ArrayList<UserEntity>();userEntities.add(new UserEntity(20L, "张三",1,1L));userEntities.add(new UserEntity(20L, "张三",1,1L));userEntities.add(new UserEntity(22L, "李四",2,2L));userEntities.add(new UserEntity(10L, "王五",3,3L));//        Map<String, List<UserEntity>> user = userEntities.stream().collect
//                (Collectors.groupingBy(UserEntity::getAge));Map<Long, List<UserEntity>> collect = userEntities.stream().collect(Collectors.groupingBy(UserEntity::getAge));System.out.println(collect+" collect");

结果

{1=[UserEntity{userId=20, username='张三', sex=1, age=1}, UserEntity{userId=20, username='张三', sex=1, age=1}],
2=[UserEntity{userId=22, username='李四', sex=2, age=2}],
3=[UserEntity{userId=10, username='王五', sex=3, age=3}]} collect

参考博客

获取集合的某个属性

这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!! 欢迎加入技术群聊。。

在这里插入图片描述

这篇关于拉姆达表达式实现对集合中的对象中的某个属性求和以及查询某个特定属性以及对特定属性求和以及针对集合根据某个属性以key-value形式分组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/796407

相关文章

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

MySQL中的交叉连接、自然连接和内连接查询详解

《MySQL中的交叉连接、自然连接和内连接查询详解》:本文主要介绍MySQL中的交叉连接、自然连接和内连接查询,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、引入二、交php叉连接(cross join)三、自然连接(naturalandroid join)四

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA

mysql的基础语句和外键查询及其语句详解(推荐)

《mysql的基础语句和外键查询及其语句详解(推荐)》:本文主要介绍mysql的基础语句和外键查询及其语句详解(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录一、mysql 基础语句1. 数据库操作 创建数据库2. 表操作 创建表3. CRUD 操作二、外键

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -