如何根据Java 8中的ID字段从一个页面上从一个页面中映射对象字段
我有两个春季页 喜欢这样:page< account>
and page< bank>
。
class Account{
String name;
String bankId;
Bank bank;
}
我想为page< account>
中的每个accode
对象提供setBank()
,并从page&lt< bank>带有相应的对象。
基于bankid
。
我想我可以使用Java 8流。
到目前为止,我尝试过:
Map<String, Account> acc = account.stream()
.collect(Collectors.toMap(Account::getBankId, a -> a));
List<Bank> bk = bank.stream()
.map(b-> new Account(b.get(b.getBankId()), bk))
.collect(Collectors.toList());
I have two Spring Pages like this : Page<Account>
and Page<Bank>
.
class Account{
String name;
String bankId;
Bank bank;
}
I would like to setBank()
for every Account
object in the Page<Account>
with the corresponding object from Page<Bank>
based on the bankId
.
I think I can use Java 8 streams for that.
So far, I've tried :
Map<String, Account> acc = account.stream()
.collect(Collectors.toMap(Account::getBankId, a -> a));
List<Bank> bk = bank.stream()
.map(b-> new Account(b.get(b.getBankId()), bk))
.collect(Collectors.toList());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想每个银行将有一个以上的帐户。因此,代替
map&lt; string,帐户&gt;
您需要map&lt; string,list&lt; account&gt;&gt;
-Charge> CharterbyBankId
。可以通过使用build-in collectorcollector.groupingby()
。Then you can map every bank to its
id
Map
withCollectors.toMap()
.And finally, set the bank object retrieved from the
bankById
map to every account in the mapaccountsByBankId
.I guess there will be more than one account in each bank. So instead of
Map<String, Account>
you need aMap<String, List<Account>>
-accountsByBankId
. Which can be created by using build-in collectorCollectors.groupingBy()
.Then you can map every bank to its
id
Map<String, Bank>
withCollectors.toMap()
.And finally, set the bank object retrieved from the
bankById
map to every account in the mapaccountsByBankId
.