1

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 I want to 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;
4
  • What are in your arraylists? strings? objects? Commented Jun 9, 2011 at 11:33
  • 1
    looks like a SQL join using teamID as your primary key - depending on the size and nature of your data / app you may want to think about using SQLLite Commented Jun 9, 2011 at 11:43
  • What are the keys and values for the HashMaps? Commented Jun 9, 2011 at 12:12
  • example, key is TeamName and Value is Germeny. Commented Jun 9, 2011 at 12:17

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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"

3 Comments

thxfor the reply, can You tell me, how can I create map of maps with key ?
thx, I have another one question, can I put this map into ListView ?
@Grins. Not in an easy way, it's better to have a parallel array with the keys, and create a ListView with that array as source.
0

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));
        }
    }
}
....  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.