public class user {
private String type;
private String year;
private String org;
private List<user> userList;
public user(String type, String year, String org) {
this.type = type;
this.year = year;
this.org = org;
}
public user() {
}
public static void main(String[] args) {
final user user1 = new user("审核通过", "2022", "编制");
user user2 = new user("审核通过", "2023", "编制");
user user3 = new user("审核不通过", "2022", "编制");
user user4 = new user("审核不通过", "2023", "编制");
user user5 = new user("审核不通过", "2024", "编制");
List<user> list = new ArrayList<>();
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
list.add(user5);
Map<String, List<user>> collect = list.stream().collect(Collectors.groupingBy(user -> user.getType()));
final Set<Map.Entry<String, List<user>>> entries = collect.entrySet();
List<user> list1 = new ArrayList<>();
final Iterator<Map.Entry<String, List<user>>> iterator = entries.iterator();
while (iterator.hasNext()) {
final Map.Entry<String, List<user>> next = iterator.next();
final user userVO = new user();
userVO.setType(next.getKey());
userVO.setUserList(next.getValue());
list1.add(userVO);
}
for (user vo : list1) {
final List<user> userList = vo.getUserList();
List<user> list2 = new ArrayList<>();
final Map<String, List<user>> collect1 = userList.stream().collect(Collectors.groupingBy(user -> user.getYear()));
final Iterator<Map.Entry<String, List<user>>> iterator1 = collect1.entrySet().iterator();
while (iterator1.hasNext()) {
final Map.Entry<String, List<user>> next = iterator1.next();
user userVO = new user();
userVO.setYear(next.getKey());
//处理value
userVO.setUserList(next.getValue());
list2.add(userVO);
}
vo.setUserList(list2);
}
System.out.println(list1);
//user user6 = new user("审核通过","2022","编制");
}
打印内容
[user(type=审核通过, year=null, org=null, userList=[user(type=null, year=2023, org=null, userList=[user(type=审核通过, year=2023, org=编制, userList=null)]), user(type=null, year=2022, org=null, userList=[user(type=审核通过, year=2022, org=编制, userList=null)])]), user(type=审核不通过, year=null, org=null, userList=[user(type=null, year=2024, org=null, userList=[user(type=审核不通过, year=2024, org=编制, userList=null)]), user(type=null, year=2023, org=null, userList=[user(type=审核不通过, year=2023, org=编制, userList=null)]), user(type=null, year=2022, org=null, userList=[user(type=审核不通过, year=2022, org=编制, userList=null)])])]