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 分钟前
Django:从入门到精通
后端·python·django
一个小坑货1 分钟前
Cargo Rust 的包管理器
开发语言·后端·rust
bluebonnet275 分钟前
【Rust练习】22.HashMap
开发语言·后端·rust
古月居GYH6 分钟前
在C++上实现反射用法
java·开发语言·c++
糖豆豆今天也要努力鸭7 分钟前
torch.__version__的torch版本和conda list的torch版本不一致
linux·pytorch·python·深度学习·conda·torch
何大春23 分钟前
【弱监督语义分割】Self-supervised Image-specific Prototype Exploration for WSSS 论文阅读
论文阅读·人工智能·python·深度学习·论文笔记·原型模式
Myli_ing29 分钟前
HTML的自动定义倒计时,这个配色存一下
前端·javascript·html
在下不上天31 分钟前
Flume日志采集系统的部署,实现flume负载均衡,flume故障恢复
大数据·开发语言·python
SEVEN-YEARS34 分钟前
深入理解TensorFlow中的形状处理函数
人工智能·python·tensorflow
EterNity_TiMe_39 分钟前
【论文复现】(CLIP)文本也能和图像配对
python·学习·算法·性能优化·数据分析·clip