小编KKl*_*ala的帖子

Java-Stream,带有重复键的toMap

所以可能有一个abc用于多次付款,现在我有:

//find abc id for each payment id
Map<Long, Integer> abcIdToPmtId = paymentController.findPaymentsByIds(pmtIds)
          .stream()
          .collect(Collectors.toMap(Payment::getAbcId, Payment::getPaymentId));
Run Code Online (Sandbox Code Playgroud)

但后来我认为这个chould有重复的键,所以我希望它返回一个

Map<Long, List<Integer>> abcIdToPmtIds 
Run Code Online (Sandbox Code Playgroud)

哪个条目将包含一个abc和他的几笔付款.

我知道我可以使用,groupingBy但我认为我只能得到Map<Long, List<Payments>>.

java java-stream

6
推荐指数
1
解决办法
2466
查看次数

java-stream-一行中列出的long列表

所以我知道我可以用两.stream行来做到这一点,但不确定我是否只能用一行来做到这一点.这就是我所拥有的:

List<Long> abcIds= abcController.findByUserIds(userIds)
      .stream()
      .map(Abc::getAbcId)
      .collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

但我希望abcIds成为一个整数列表,因为稍后我会使用它的其他函数.我知道我可以写这样的另一行来将List of Long转换为整数列表:

List<Integer> abcIntIds= abcIds.stream()
           .map(Long::intValue)
           .collec?t(Collectors.toList(??));
Run Code Online (Sandbox Code Playgroud)

但有没有办法把它写得更优雅?

java java-8 long-integer java-stream

2
推荐指数
1
解决办法
112
查看次数

标签 统计

java ×2

java-stream ×2

java-8 ×1

long-integer ×1