本文主要是介绍Java8-Stream 流基本应用-groupBy进行分组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
groupBy进行分组
@Testpublic 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);}
分组结果
分组之前
[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)]
分组之后
{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)]}
这篇关于Java8-Stream 流基本应用-groupBy进行分组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!