java - How to count key values in a HashMap -


i writing game , i'm stuck..

we have 2 teams

public enum teamtype {    team_one,    team_two; }  private optional<teamtype> team; 

and hashmap

private map<teamtype, player> teamplayers = new hashmap<>(); 

how count amount of players within team type? (returning numeric value)

teamplayers.get(teamtype.zamorak).size();  

is not possible.

if list of players:

private map<teamtype, list<player>> teamplayers = new hashmap<>(); 

this work:

teamplayers.get(teamtype.team_one).size(); 

of course you'd need initialize empty player list:

teamplayers.put(teamtype.team_one, new arraylist<player>()); teamplayers.put(teamtype.team_two, new arraylist<player>()); 

then add players team:

teamplayers.get(teamtype.team_one).add(new player()); 

Comments

Popular posts from this blog

Qt4: how to send QString inside a struct via QSharedMemory -

node.js - NodeJS remote terminal to Dropbear OpenWRT-Server -

python - jinja2: TemplateSyntaxError: expected token ',', got 'string' -