[ { "id": 1, "groupName": "test-wu", "description": "sadsaffdsa", "dingId": "401021613327413104", "userName": "武福根", "email": "eureka.wu@ibaiqiu.com", "createTime": "2023-05-31 03:34:51", "createName": "admin", "updateTime": "2023-05-31 03:34:51", "updateName": "admin" }, { "id": 2, "groupName": "test-wu", "description": "sadsaffdsa", "dingId": "322610312920535304", "userName": "俞永波", "email": "ron.yu@ibaiqiu.com", "createTime": "2023-05-31 03:34:51", "createName": "admin", "updateTime": "2023-05-31 03:34:51", "updateName": "admin" }, { "id": 3, "groupName": "ron-test", "description": "测试2", "dingId": "306208510726220319", "userName": "杨志亮", "email": "sean.yang@ibaiqiu.com", "createTime": "2023-05-31 03:44:12", "createName": "admin", "updateTime": "2023-05-31 03:44:12", "updateName": "admin" }, { "id": 4, "groupName": "ron-test", "description": "测试2", "dingId": "314447695938075811", "userName": "雍睿涵", "email": "ruihan.yong@ibaiqiu.com", "createTime": "2023-05-31 03:44:12", "createName": "admin", "updateTime": "2023-05-31 03:44:12", "updateName": "admin" }, { "id": 5, "groupName": "ron-test", "description": "测试2", "dingId": "066357011133738415", "userName": "蔡秋丹", "email": "qiudan.cai@ibaiqiu.com", "createTime": "2023-05-31 03:44:12", "createName": "admin", "updateTime": "2023-05-31 03:44:12", "updateName": "admin" }, { "id": 6, "groupName": "ron-test", "description": "测试2", "dingId": "322610312920535304", "userName": "俞永波", "email": "ron.yu@ibaiqiu.com", "createTime": "2023-05-31 03:34:51", "createName": "admin", "updateTime": "2023-05-31 03:34:51", "updateName": "admin" } ] stream分组,将
时间: 2023-07-03 17:22:03 浏览: 161
Test-Repro:测试
根据groupName分组,并将每个分组中的元素的id和userName字段提取出来,放入一个Map中。可以使用Java 8的Stream API中的groupingBy方法结合mapping方法来实现,示例代码如下:
```
List<User> userList = ... // 假设已经初始化了一个User列表
Map<String, List<Map<String, Object>>> groupedByGroupName = userList.stream()
.collect(Collectors.groupingBy(User::getGroupName,
Collectors.mapping(user -> {
Map<String, Object> map = new HashMap<>();
map.put("id", user.getId());
map.put("userName", user.getUserName());
return map;
}, Collectors.toList())));
```
上述代码中,groupedByGroupName就是一个Map对象,key为groupName,value为该groupName对应的所有用户的id和userName字段的Map对象列表。需要注意的是,上述代码中的User类需要有getGroupName、getId和getUserName三个方法来获取groupName、id和userName。
阅读全文