HashMap陷入循环

时间:2018-12-10 11:38:29

标签: java for-loop 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中,放置存储库的名称,但不放置userCommitCount映射。

1 个答案:

答案 0 :(得分:0)

您要打印两次哈希图:

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

也许您应该只打印一次?