您可以执行
groupingBy两次:
Map<Integer, Map<Integer, Long>> map = bids.stream().collect( groupingBy(Bid::getBidderUserId, groupingBy(Bid::getAuctionId, counting())));
这样,您就可以确定每个用户在每次拍卖中有多少个出价。因此,内部地图的大小就是用户参与的拍卖次数。如果您不需要其他信息,可以执行以下操作:
Map<Integer, Integer> map = bids.stream().collect( groupingBy( Bid::getBidderUserId, collectingAndThen( groupingBy(Bid::getAuctionId, counting()), Map::size)));
这正是您所需要的:将用户映射到参与的拍卖数量。
更新: 还有一个更接近您的示例的类似解决方案:
Map<Integer, Integer> map = bids.stream().collect( groupingBy( Bid::getBidderUserId, collectingAndThen( mapping(Bid::getAuctionId, toSet()), Set::size)));



