【发布时间】:2018-05-13 08:36:50
【问题描述】:
我有 3 个课程:Agency、Team 和 Agent。 Agent 属于 Team,而 Team 又属于 Agency。
我需要做一份报告,计算每个 Agency 上我有多少代理。
我没有数据库,只有ArrayLists 有以下数据:
List<Agent> agents
List<Team> teams
List<Agency> agencies
在Agent 类上,我有getTeam(),它有getAgency()(来自Team 类)。
我想这样做:
for (Agency ag : agencies) {
for (Agent a : agents) {
if (a.getTeam() != null) {
if (a.getTeam().getAgency() == ag) {
teste = "Agency from agent" + a.getTeam().getAgency().getCode() + "Agency from agency" + ag.getCode();
}
}
}
}
但我不知道如何将每个 Agency 分开计算。
所以我尝试了:
Map<String, Long> agentsAtAgency =
agents.stream()
.collect(Collectors.groupingBy(Agent::getTeam, Collectors.counting()));
但我无法从Agent 获得代理,因为我需要getTeam().getAgency() 而这张地图不允许我这样做。
总结:
我有代理:A、B、C。
我有代理:X 与代理 A 相关,Y 与代理 A 相关,Z 与代理 B 相关。
我需要出示:A 机构:2 名代理人/B 机构:1 名代理人/C 机构:0 人
有人可以帮帮我吗?
【问题讨论】:
标签: java group-by java-8 counting collectors