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)]}
相关推荐
uhakadotcom29 分钟前
Astro 框架:快速构建内容驱动型网站的利器
前端·javascript·面试
uhakadotcom33 分钟前
了解Nest.js和Next.js:如何选择合适的框架
前端·javascript·面试
uhakadotcom34 分钟前
React与Next.js:基础知识及应用场景
前端·面试·github
uhakadotcom43 分钟前
Remix 框架:性能与易用性的完美结合
前端·javascript·面试
uhakadotcom1 小时前
Node.js 包管理器:npm vs pnpm
前端·javascript·面试
LaoZhangAI2 小时前
2025最全GPT-4o图像生成API指南:官方接口配置+15个实用提示词【保姆级教程】
前端
ONE_Gua2 小时前
chromium魔改——CDP(Chrome DevTools Protocol)检测01
前端·后端·爬虫
LaoZhangAI2 小时前
2025最全Cherry Studio使用MCP指南:8种强大工具配置方法与实战案例
前端
咖啡教室2 小时前
前端开发日常工作每日记录笔记(2019至2024合集)
前端·javascript
溪饱鱼2 小时前
Nuxt3能上生产吗?
前端