How to calculate key values ​​in HashMap

I mostly write this for the game and I'm stuck.

We mainly have 2 teams

public enum TeamType {
   TEAM_ONE,
   TEAM_TWO;
}

private Optional<TeamType> team;

and hashmap

private Map<TeamType, Player> teamPlayers = new HashMap<>();

how will we count the number of players in a group? (Return a numeric value)

teamPlayers.get(TeamType.ZAMORAK).size(); 

impossible.

-1
source share
2 answers

If this was a list of players:

private Map<TeamType, List<Player>> teamPlayers = new HashMap<>();

This will work:

teamPlayers.get(TeamType.TEAM_ONE).size();

Of course, you will need to initialize an empty list of players:

teamPlayers.put(TeamType.TEAM_ONE, new ArrayList<Player>());
teamPlayers.put(TeamType.TEAM_TWO, new ArrayList<Player>());

Then add players to the team:

teamPlayers.get(TeamType.TEAM_ONE).add(new Player());
0
source

Java HashMaps . , . MultiHashMap, , , .

MultiHashMap, teamPlayers.get(TeamType.ZAMORAK).size(); ZAMORAK.

+1

All Articles