public static void main(String[] args) { User user = new User(); user.setId(1L); user.setNickName("账单"); User user2 = new User(); user2.setId(1L); user2.setNickName("电话"); List<User> userList = Lists.newArrayList(user, user2); Stream<User> stream = userList.stream(); Map<Long,List<User>> map = stream.collect(Collectors.groupingBy(User::getId)); System.out.println(JSONObject.toJSONString(map)); }
输出:{1:[{"id":1,"nickName":"账单"},{"id":1,"nickName":"电话"}]}