java 8 - stream, map and count distinct(java 8 - 流,映射和计数不同)
问题描述
我第一次尝试使用 java 8 流...
My first attempt with java 8 streams...
我有一个对象 Bid,它代表用户对拍卖中的物品的出价.我有一个出价列表,我想制作一张地图,计算用户出价的(不同的)拍卖次数.
I have an object Bid, which represents a bid of a user for an item in an auction. i have a list of bids, and i want to make a map that counts in how many (distinct) auctions the user made a bid.
这是我的看法:
bids.stream()
.collect(
Collectors.groupingBy(
bid -> Bid::getBidderUserId,
mapping(Bid::getAuctionId, Collectors.toSet())
)
).entrySet().stream().collect(Collectors.toMap(
e-> e.getKey(),e -> e.getValue().size())
);
它有效,但我觉得我在作弊,因为我流式传输地图的条目集,而不是对初始流进行操作......必须是这样做的更正确的方式,但我不能想不通……
It works, but i feel like i'm cheating, cause i stream the entry sets of the map, instead of doing a manipulation on the initial stream... must be a more correct way of doing this, but i couldn't figure it out...
谢谢
推荐答案
你可以执行两次groupingBy
:
Map<Integer, Map<Integer, Long>> map = bids.stream().collect(
groupingBy(Bid::getBidderUserId,
groupingBy(Bid::getAuctionId, counting())));
这样您就可以知道每个用户在每次拍卖中的出价数.所以内部地图的大小就是用户参与的拍卖次数.如果您不需要其他信息,可以这样做:
This way you have how many bids each user has in each auction. So the size of internal map is the number of auctions the user participated. If you don't need the additional information, you can do this:
Map<Integer, Integer> map = bids.stream().collect(
groupingBy(
Bid::getBidderUserId,
collectingAndThen(
groupingBy(Bid::getAuctionId, counting()),
Map::size)));
这正是您所需要的:将用户映射到用户参与的拍卖数量.
This is exactly what you need: mapping of users to number of auctions user participated.
更新:还有与您的示例更接近的类似解决方案:
Update: there's also similar solution which is closer to your example:
Map<Integer, Integer> map = bids.stream().collect(
groupingBy(
Bid::getBidderUserId,
collectingAndThen(
mapping(Bid::getAuctionId, toSet()),
Set::size)));
这篇关于java 8 - 流,映射和计数不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java 8 - 流,映射和计数不同
基础教程推荐
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01