【发布时间】:2021-09-19 01:05:28
【问题描述】:
我有以下对象列表
class Account {
int id;
String type;
int balance;
Customer customer;
// getters setters
}
class Customer {
int customerID;
}
List<Account> accounts = new ArrayList<>();
accounts.add(new Account(1, "abc", 17998210, new Customer(190)));
accounts.add(new Account(2, "hsj", 6786179, new Customer(190)));
accounts.add(new Account(4, "ioip", 246179, new Customer(191)));
accounts.add(new Account(4, "ewrew", 90179, new Customer(191)));
我想将上面的内容转移到 Map,key 应该是 customerID,values 应该是 Account 列表
Map<Integer, List<Account>>
Key Value
190 -> Account(1, "abc", 17998210, 190)
Account(2, "hsj", 6786179, 190)
191 -> Account(4, "ioip", 246179, 191)
Account(4, "ewrew", 90179, 191)
如何做到这一点?
【问题讨论】:
标签: java java-8 java-stream grouping