如何将自定义对象列表重新组合到 Java 中的地图中?
How to regroup a list of custom objects into a map in Java?
我有以下对象列表
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,value 应该是 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)
如何实现?
您可以使用 Collectors.groupingBy
.
Map<Integer, List<Account>> map =
accounts.stream().collect(Collectors.groupingBy(Account::getCustomerID));
List<Account> accounts = new ArrayList<>();
accounts.add(new Account(1, "abc", 17998210, 190));
accounts.add(new Account(2, "hsj", 6786179, 190));
accounts.add(new Account(4, "ioip", 246179, 191));
accounts.add(new Account(4, "ewrew", 90179, 191));
Map<Integer, List<Account>> accountsMap = new HashMap<>();
for (Account account : accounts) {
accountsMap.computeIfAbsent(account.customerID, k -> new ArrayList<>()).add(account);
}
为清楚起见,我更愿意使用具有三个参数的 Collectors.toMap
方法:
Map<Integer, List<Account>> map = accounts.stream()
.collect(Collectors.toMap(
// key - customerID
e -> e.getCustomer().getCustomerID(),
// value - List<Account>
e -> List.of(e),
// merge two lists
(l1, l2) -> Stream.of(l1, l2)
.flatMap(List::stream)
.collect(Collectors.toList())));
我有以下对象列表
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,value 应该是 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)
如何实现?
您可以使用 Collectors.groupingBy
.
Map<Integer, List<Account>> map =
accounts.stream().collect(Collectors.groupingBy(Account::getCustomerID));
List<Account> accounts = new ArrayList<>();
accounts.add(new Account(1, "abc", 17998210, 190));
accounts.add(new Account(2, "hsj", 6786179, 190));
accounts.add(new Account(4, "ioip", 246179, 191));
accounts.add(new Account(4, "ewrew", 90179, 191));
Map<Integer, List<Account>> accountsMap = new HashMap<>();
for (Account account : accounts) {
accountsMap.computeIfAbsent(account.customerID, k -> new ArrayList<>()).add(account);
}
为清楚起见,我更愿意使用具有三个参数的 Collectors.toMap
方法:
Map<Integer, List<Account>> map = accounts.stream()
.collect(Collectors.toMap(
// key - customerID
e -> e.getCustomer().getCustomerID(),
// value - List<Account>
e -> List.of(e),
// merge two lists
(l1, l2) -> Stream.of(l1, l2)
.flatMap(List::stream)
.collect(Collectors.toList())));