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);
}
分组结果
分组之前
[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)]}