我有三个类:Agency(机构),Team(团队)和Agent(代理人)。Agent属于一个 Team,而 Team 又属于一个 Agency。
我需要生成一份报告,统计每个 Agency 拥有多少个 Agent。 我没有数据库,只有以下数据的 ArrayList:
在
我想要实现的是:
我需要生成一份报告,统计每个 Agency 拥有多少个 Agent。 我没有数据库,只有以下数据的 ArrayList:
List<Agent> agents
List<Team> teams
List<Agency> agencies
在
Agent
类上,我有一个方法getTeam()
,它又调用了Team
类的方法getAgency()
。我想要实现的是:
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();
}
}
}
}
但我不知道如何为每个机构
单独计数。
所以我尝试了:
Map<String, Long> agentsAtAgency =
agents.stream()
.collect(Collectors.groupingBy(Agent::getTeam, Collectors.counting()));
但是我无法从Agent
中获取机构,因为我需要使用getTeam().getAgency()
,而这个Map不允许我这样做。
总结一下:
我有机构:A、B、C。
我有代理:X与机构A相关联,Y与机构A相关联,Z与机构B相关联。
我需要展示:机构A:2名代理/机构B:1名代理/机构C:0
请问有人可以帮我吗?
for(Map.Entry<String, Long> entry : agentsAtAgency) { System.out.println("机构" + entry.getKey() + "有" + entry.getValue() + "名代理商"); }
- EranagentsAtAgency.forEach((agency, count) -> System.out.println("机构" + agency + "有" + count + "个特工");
- Holger