How do you create a Two-Dimensional ArrayList?

Android, create one arraylist from two arraylists

  • I have a problem with comparing two arraylist, my first arraylist looks like this: {TeamID=4, Group=A, TeamName=Germany} {TeamID=6, Group=A, TeamName=Turkey} my second list: {TeamID=4, Chance=99.9%} {TeamID=6, Chance=38.4%} and then a want create one list, which will look like this: {TeamID=4, Group=A, TeamName=Germany Chance=99.9%} {TeamID=6, Group=A, TeamName=Turkey Chance=38.4%} Can You help me ? First List: private ArrayList<HashMap<String, String>> TeamList = this.xml.TeamListList; Second: private ArrayList<HashMap<String, String>> QChanceList = this.xml.QChanceListList;

  • Answer:

    Here's one idea. For each team I search for the corresponding entry in the chances list, then I put all entries from both maps into a new one. public static List<Map<String, String>> merge( List<Map<String, String>> teams, List<Map<String, String>> chances) { // create the result List<Map<String, String>> result = new ArrayList<Map<String, String>>(); // now we assume that for each team there is one chance (valid?) for (Map<String, String> team:teams) { boolean success = false; Map<String, String> combined = new HashMap<String, String>(); combined.putAll(team); String id = team.get("TeamID"); // now we have to find the "chance" map for (Map<String, String> chance:chances) { if (chance.get("TeamID").equals(id)) { combined.putAll(chance); boolean success = true; break; } } if (!success) { // there was no entry in chances map with this id!! -> handle problem } result.add(combined); } return result; } (This example is not very fail-safe, it asserts, that all maps have values for "TeamId", I just demonstrate that something has to be done in case of illegal input, like an incomplete chances list)

Dawid Sajdak at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Team list should be a map of maps. Use team id as key in the outer map (if you don't want to change to a database). Merging the entries would then be very easy. Iterate over chance list, get the team from the teams map, and use teamMap.putAll(mapFromChanceList) Edit: Update with example. Map<String, Map<String, String> teams = new HashMap<String, Map<String, String>(); Map<String, String> team = new Map<String, String>(); //populate the team map, and with TeamID, TeamName etc, then do something like this. teams.put(team.get("TeamID"), team); //You get a team by doing: Map<String, String> team = teams.get(teamId); //where teamId is e.g. "4"

Kaj

Similarly to Andreas_D, here's another way to skin that cat. I would like to say that I still agree with Dori, this really does look like database data. I'd recommend you use a database and save yourself the headache of solving this problem that's already been solved. Either that or work with the owner of the API you're using to get this data and get them to use a proper database query to merge it before it ever gets to you. Forgoing that, your best bet is to do something like this to get to that point so that you can use the utilities of hashmap. .... HashMap<String, HashMap<String, String>> unifiedMap = new HashMap<String, HashMap<String, String>>(); for (HashMap<String, String> teamMap : teamList) { String teamId = teamMap.get("TeamID"); HashMap<String, String> mapFromUnified = unifiedMap.get(teamId); if (mapFromUnified == null) { unifiedMap.put(teamId, teamMap); } else { for (String key : teamMap.keySet()) { // this will be a race condition, the last one in wins - good luck! mapFromUnified.put(key, teamMap.get(key)); } } } for (HashMap<String, String> teamMap : chanceList) { String teamId = teamMap.get("TeamID"); HashMap<String, String> mapFromUnified = unifiedMap.get(teamId); if (mapFromUnified == null) { unifiedMap.put(teamId, teamMap); } else { for (String key : teamMap.keySet()) { // this will be a race condition, the last one in wins - good luck! mapFromUnified.put(key, teamMap.get(key)); } } } ....

Travis

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.