How can I store ArrayList in an HashMap and add values to that ArrayList dynamically | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I store ArrayList in an HashMap and add values to that ArrayList dynamically

Java Related query

17th Jun 2016, 7:02 AM
K R Hitesh
K R Hitesh - avatar
2 Answers
0
sry i dont know
31st Aug 2016, 1:03 AM
Talia
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"); } } }
3rd Nov 2016, 11:18 AM
K R Hitesh
K R Hitesh - avatar