JAVA8 stream 分组 groupby使用

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 userList = Lists.newArrayList(user, user2);
    Stream stream = userList.stream();

    Map> map = stream.collect(Collectors.groupingBy(User::getId));
    System.out.println(JSONObject.toJSONString(map));
}

 

输出:{1:[{"id":1,"nickName":"账单"},{"id":1,"nickName":"电话"}]}

你可能感兴趣的:(java)