-2

Let's say we have our map

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

and we initialize them

teamPlayers.put(TeamType.ONE, new ArrayList<Player>());
teamPlayers.put(TeamType.TWO, new ArrayList<Player>());

and we store some values in them

teamPlayers.get(TeamType.ONE).add(player);
teamPlayers.get(TeamType.TWO).add(player);

is there a way to return the amount of players in both teams besides doing

int totalPlayers = teamPlayers.get(TeamType.ONE).size() + teamPlayers.get(TeamType.Two).size();

because this can get very cluttered after having more then 4 teams. Keep in mind TeamType is an enum.

Jason Qlueses
  • 43
  • 1
  • 6
  • Let's say I have 4 shelves. The first shelf supports 1 book, the second shelf supports 3 books, the third shelf supports 5 books, and the fourth shelf supports 2 books. How would you count the total number of supported books across all shelves? – Sotirios Delimanolis Jul 20 '15 at 22:29
  • So you have a map like this `Map` and then the value is of type list? Does that work for you? – Tom Jul 20 '15 at 22:51

1 Answers1

1
 int total =teamPlayers.get(TeamType.TEAM_ONE).size() + teamPlayers.get(TeamType.TEAM_TWO).size()

should work. Instead of referencing your old post can you post your current code? hard to know what changes youve made and what suggestions you took from the last post.

updated answer for iterating through every entry in your TeamType enum:

int total = 0;
for(TeamType type : TeamType.values()){
    total += teamPlayers.get(type).size();
}

so the for each loop will iterate through each entry in your enum, and total will accumulate the number of players for each team

GregH
  • 5,125
  • 8
  • 55
  • 109
  • teamPlayers.get(TeamType.TEAM_ONE) would return a Player and it makes sense that you wont be able to call size() on a Player – Vivin Jul 20 '15 at 22:33
  • well he references his last post which provides an accepted answer to use a list of players instead of only one player which is part of the reason i requested that he posted his current code. Hard to tell what OP is doing without any code – GregH Jul 20 '15 at 22:33
  • @peggy I am sorry guys. Did not see that!! – Vivin Jul 20 '15 at 22:35
  • @Vwin no problem at all. – GregH Jul 20 '15 at 22:35
  • The answer is correct. :) – Vivin Jul 20 '15 at 22:36
  • Nah what Vwin stated works if you make it teamPlayers.get(...).size() but what if we had lets say 10 teams, doing it like that will get it very cluttered.. – Jason Qlueses Jul 20 '15 at 22:36
  • Yes you need to iterate through the map – Vivin Jul 20 '15 at 22:37
  • @peggy i've done it. – Jason Qlueses Jul 20 '15 at 22:47
  • ok so you are using lists, so iterate through all of your teams and add their sizes as shown above – GregH Jul 20 '15 at 22:50
  • @peggy can you show an example.. as shown above means nothing because nobody has yet posted an answer to my problem :/ – Jason Qlueses Jul 20 '15 at 22:52
  • updated my answer. and if you were to post clear questions which include your code from the beginning, we couldve gotten here a lot faster :) all is good though welcome to Stack Overflow :D – GregH Jul 20 '15 at 22:54
  • I wonder what will happen if there are four possible teams in the enum, but only two team play in a given game ... maybe a `NullPointerException`? – Tom Jul 20 '15 at 23:00
  • my understanding is he does not want to count the players that exist in a game, but all players from all teams – GregH Jul 20 '15 at 23:01
  • Well, then let's hope so. – Tom Jul 20 '15 at 23:07
  • @peggy isn't there a way to make it a one liner with a lambda? Wouldn't 'code'public static Optional getCount() { return Arrays.stream(values())..... }'code' – Jason Qlueses Jul 20 '15 at 23:16