[Solved] Inputting data in hash map of hash map in one loop or two single loops


Your problem description is unclear, but given your input file, the following probably does what you need:

HashMap<String, HashMap<String, String>> repoUserMap = new HashMap<>();

for (int i = 0; i < data.size(); i++) { //data is an arraylist
    String[] seq = data.get(i).split(",");
    String repo = seq[0];

    // Lookup the 2nd-level map, and create it if it doesn't exist yet.
    HashMap<String, String> userMap = repoUserMap.get(repo);
    if (userMap == null) {
        userMap = new HashMap<>();
        repoUserMap.put(repo, userMap);
    }

    usermap.put(seq[1],"seq[2] + "," + seq[3] + "," + seq[4] + "," + seq[5] + 
            "," + seq[6]    + "," + seq[7] + "," + seq[8] + "," + seq[9]");
 }

The middle section with the comment is the part you were missing (conceptually).


If there was a need to merge counts from multiple input lines for the same user and repo, then you would do it at the last line of the loop. However that is not indicated in your code, your example or your description, so I am assuming it is not needed.

I would also point out that if you are doing this so that you can lookup the counts quickly, then you should be storing them as an array of integers, not as a string that needs to be split and parsed each time you lookup a count.

6

solved Inputting data in hash map of hash map in one loop or two single loops