0
How can I store ArrayList in an HashMap and add values to that ArrayList dynamically
Java Related query
2 Respostas
0
sry i dont know
0
Example for above using Test cricket example :
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Program
{
private static int NO_OF_TEAM = 2;
private static final String[] TEAMS_NAME = { "India", "Aus" };
private static final int NO_OF_PLAYERS_IN_TEAM = 11;
private static HashMap<String, ArrayList<?>> teamsScore = new HashMap<>();
public static void main(String[] args) {
while (NO_OF_TEAM-- > 0) {
ArrayList<Long> playerIndividualScore = new ArrayList<>();
for (int player = 1; player <= NO_OF_PLAYERS_IN_TEAM; player++) {
playerIndividualScore.add(Math.round(Math.random() * 100));
}
teamsScore.put(TEAMS_NAME[NO_OF_TEAM], playerIndividualScore);
}
for (Map.Entry team : teamsScore.entrySet()) {
int teamTotal = 0;
for (long score : (ArrayList<Long>) team.getValue()) {
teamTotal += score;
}
System.out.println(team.getKey() + "\n" + "Score Board " + team.getValue().toString() + "\n"
+ "Team Total " + teamTotal + "\n");
}
}
}