HashMap放入循环

for (int i = 0; i < projectList.size(); i++) {
        String projectName = projectList.get(i);
        String url = "https://api.github.com/repos/apache/" + projectName + "/commits";

        try {
            data = Jsoup.connect(url).timeout(30000).ignoreContentType(true).execute().body();
            System.out.println(url);
            // System.out.println(data);

        } catch (Exception e) {
            e.printStackTrace();
        }
        objArray = new JSONArray(data);

        for (int i1 = 0; i1 < objArray.length(); i1++) {
            obj = objArray.getJSONObject(i1);               

            if (!obj.isNull("committer")) {
                obj1 = obj.getJSONObject("committer");
                commiterName = obj1.getString("login");
            }
            else {
                continue;
            }
            if (!UserCommitCount.containsKey(commiterName)) {
                UserCommitCount.put(commiterName, 1);
            } else {
                UserCommitCount.put(commiterName, UserCommitCount.get(commiterName) + 1);
            }

        }
        UserCommitCount = sortByComparator(UserCommitCount, DESC);
        UserCommitCount = resizeUserCommitCount(UserCommitCount);
        printHashMap(UserCommitCount);
        System.out.println(projectName + "!!!!");
        printHashMap(UserCommitCount);

        RepoNameCommitterMap.put(projectName, UserCommitCount);
        UserCommitCount.clear();
        System.out.println(projectName+ "AFTER");
        printHashMap(UserCommitCount);
        printNestedHashMap(RepoNameCommitterMap);
    }

所以即时尝试将数据存储在Hashmap中,输出就像这样

https://api.github.com/repos/apache/zookeeper/commits

hanm - 4

anmolnar - 23

lvfangmin - 3

zookeeper!!!!

hanm - 4

anmolnar - 23

lvfangmin - 3


zookeeper - {}
https://api.github.com/repos/apache/camel/commits

davsclaus - 9

oscerd - 20

PascalSchumacher - 1

camel!!!!

davsclaus - 9

oscerd - 20

PascalSchumacher - 1

camelAFTER

zookeeper - {davsclaus=9, oscerd=20, PascalSchumacher=1}

camel - {}

https://api.github.com/repos/apache/httpd/commits

minfrin - 1

elukey - 2

ylavic - 6

jfclere - 3

notroj - 4

jimjag - 1

httpd!!!!

minfrin - 1

elukey - 2

ylavic - 6

jfclere - 3

notroj - 4

jimjag - 1

httpdAFTER

zookeeper - {davsclaus=9, oscerd=20, PascalSchumacher=1}

camel - {minfrin=1, elukey=2, ylavic=6, jfclere=3, notroj=4, jimjag=1}

httpd - {}

然后继续 . 您会看到第一个put命令键被放置但hashMap没有,它会转移下一个迭代 . 我不明白为什么会这样 . 任何人都可以帮我修复它吗?

在第一次迭代ReponameCommiter.put中,放置repo的名称但不放置userCommitCount Map .

回答(1)

2 years ago

您正在打印哈希映射两次:

printHashMap(UserCommitCount);
System.out.println(projectName + "!!!!");
printHashMap(UserCommitCount);

也许你应该只打印一次?