fork download
  1. import java.util.*;
  2. import java.util.stream.*;
  3. public class Main {
  4. public static void main(String[] args) {
  5. List<Account> accounts = new ArrayList<>();
  6. accounts.add(new Account(1, "abc", 17998210, 190));
  7. accounts.add(new Account(2, "hsj", 6786179, 190));
  8. accounts.add(new Account(4, "ioip", 246179, 191));
  9. accounts.add(new Account(4, "ewrew", 90179, 191));
  10. Map<Integer, List<Account>> map = accounts.stream().collect(Collectors.groupingBy(Account::getCustomerID));
  11. System.out.println(map);
  12. }
  13. }
  14. class Account{
  15. int id;
  16. String type;
  17. int balance;
  18. int customerID;
  19. public Account(int id, String type, int balance, int customerID){
  20. this.id = id;
  21. this.type = type;
  22. this.balance = balance;
  23. this.customerID = customerID;
  24. }
  25. public int getId() {
  26. return id;
  27. }
  28. public void setId(int id) {
  29. this.id = id;
  30. }
  31. public String getType() {
  32. return type;
  33. }
  34. public void setType(String type) {
  35. this.type = type;
  36. }
  37. public int getBalance() {
  38. return balance;
  39. }
  40. public void setBalance(int balance) {
  41. this.balance = balance;
  42. }
  43. public int getCustomerID() {
  44. return customerID;
  45. }
  46. public void setCustomerID(int customerID) {
  47. this.customerID = customerID;
  48. }
  49. @Override
  50. public String toString() {
  51. return "Account [id=" + id + ", type=" + type + ", balance=" + balance + ", customerID=" + customerID + "]";
  52. }
  53. }
Success #stdin #stdout 0.2s 57288KB
stdin
Standard input is empty
stdout
{190=[Account [id=1, type=abc, balance=17998210, customerID=190], Account [id=2, type=hsj, balance=6786179, customerID=190]], 191=[Account [id=4, type=ioip, balance=246179, customerID=191], Account [id=4, type=ewrew, balance=90179, customerID=191]]}