Java8-Stream 流基本应用-groupBy进行分组

groupBy进行分组

复制代码
 @Test
 public void testStreamGroupBy(){
    List<UserInfoModel> result=new ArrayList<>();
  for (int i = 0; i < 10; i++) {
   UserInfoModel user=new UserInfoModel();
   user.setUserId(i+"");
   user.setUserName("kangshihang");
   result.add(user);
  }

  for (int i = 0; i < 10; i++) {
   UserInfoModel user=new UserInfoModel();
   user.setUserId(i+"");
   user.setUserName("kangshihang1");
   result.add(user);
  }
  System.out.println("分组之前的数组内容result"+result);

  //使用stream的groupBy指定属性进行分组
  Map<String, List<UserInfoModel>> collect = result.stream().collect(Collectors.groupingBy(UserInfoModel::getUserName));

  System.out.println("分组之后的数组内容result"+collect);

 }

分组结果

分组之前

java 复制代码
[UserInfoModel(userId=0, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=1, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=2, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=3, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=4, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=5, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=6, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=7, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=8, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=9, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=0, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=1, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=2, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=3, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=4, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=5, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=6, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=7, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=8, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=9, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null)]

分组之后

java 复制代码
{kangshihang1=[UserInfoModel(userId=0, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=1, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=2, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=3, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=4, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=5, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=6, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=7, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=8, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=9, userName=kangshihang1, userPhone=null, weixinId=null, userPassword=null)],

kangshihang=[UserInfoModel(userId=0, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=1, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=2, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=3, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=4, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=5, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=6, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=7, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=8, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null), UserInfoModel(userId=9, userName=kangshihang, userPhone=null, weixinId=null, userPassword=null)]}
相关推荐
跳跳糖炒酸奶1 分钟前
第四章、Isaacsim在GUI中构建机器人(1): 添加简单对象
人工智能·python·ubuntu·机器人
Zfox_4 分钟前
【C++项目】从零实现RPC框架「四」:业务层实现与项目使用
linux·开发语言·c++·rpc·项目
我想吃余7 分钟前
【C++篇】类与对象(上篇):从面向过程到面向对象的跨越
开发语言·c++
Niuguangshuo11 分钟前
Python设计模式:克隆模式
java·开发语言·python
阳树阳树18 分钟前
signal-新的状态管理模式
前端·javascript
双叶83618 分钟前
(C语言)单链表(1.0)(单链表教程)(数据结构,指针)
c语言·开发语言·数据结构·算法·游戏
fakaifa19 分钟前
beikeshop多商户跨境电商独立站最新版v1.6.0版本源码
前端·小程序·uni-app·php·beikeshop多商户·beikeshop跨境电商
想睡hhh20 分钟前
c++概念——入门基础概念
开发语言·c++
爱摄影的程序猿21 分钟前
如何基于 Django-Vue-Admin 快速二次开发?高效后台管理系统实战指南(附完整代码)
vue.js·python·django
肖永威29 分钟前
python列表常用方法大全
开发语言·python