首先,標題并沒有表達我真正想問的。我想知道的是,我如何制作地圖,為多個用戶收集他們的資料,然后將它們組合在一起。我目前使用兩個串列,一個用于用戶姓名,另一個用于他們的作品。我嘗試使用 map.put 但它一直覆寫上一個條目。所以我想獲得如下;
期望的輸出:
{user1 = work1, work2, work3 , user2 = work1, work2 , userN = workN}
電流輸出:
{[user1, user2, user3, user4]=[work1, work2, work3, work4, work5 (user1) , work1 (user2), work1, work2, work3 ( user3 )]}
這是我目前用來實作上述目標的代碼。
private static Map<List<String>, List<String>> repositoriesUserData = new HashMap<>();
private static Set<String> collaboratorNames = new HashSet<>();
public static void main(String[] args) throws Exception {
login();
getCollabs(GITHUB_REPO_NAME);
repositoriesUnderUser();
}
public GitManager(String AUTH, String USERNAME, String REPO_NAME) throws IOException {
this.GITHUB_LOGIN = USERNAME;
this.GITHUB_OAUTH = AUTH;
this.GITHUB_REPO_NAME = REPO_NAME;
this.githubLogin = new GitHubBuilder().withOAuthToken(this.GITHUB_OAUTH, this.GITHUB_LOGIN).build();
this.userOfLogin = this.githubLogin.getUser(GITHUB_LOGIN);
}
public static void login() throws IOException {
new GitManager(GIT_TOKEN, GIT_LOGIN, GITHUB_REPO_NAME);
connect();
}
public static void connect() throws IOException {
if (githubLogin.isCredentialValid()) {
valid = true;
githubLogin.connect(GITHUB_LOGIN, GITHUB_OAUTH);
userOfLogin = githubLogin.getUser(GITHUB_LOGIN);
}
}
public static String getCollabs(String repositoryName) throws IOException {
GHRepository collaboratorsRepository = userOfLogin.getRepository(repositoryName);
collaboratorNames = collaboratorsRepository.getCollaboratorNames();
String collaborators = collaboratorNames.toString();
System.out.println("Collaborators for the following Repository: " repositoryName "\nAre: " collaborators);
String out = "Collaborators for the following Repository: " repositoryName "\nAre: " collaborators;
return out;
}
public static List<String> fillList() {
List<String> collaborators = new ArrayList<>();
collaboratorNames.forEach(s -> {
collaborators.add(s);
});
return collaborators;
}
public static String repositoriesUnderUser() throws IOException {
GHUser user;
List<String> names = new ArrayList<>();
List<String> repoNames = new ArrayList<>();
for (int i = 0; i < fillList().size(); i ) {
user = githubLogin.getUser(fillList().get(i));
Map<String, GHRepository> temp = user.getRepositories();
names.add(user.getLogin());
temp.forEach((c, b) -> {
repoNames.add(b.getName());
});
}
repositoriesUserData.put(names,repoNames);
System.out.println(repositoriesUserData);
return "temporaryReturn";
}
感謝所有幫助!
uj5u.com熱心網友回復:
我會試一試(有問題的代碼仍然不適合我):
如果我理解正確,您需要一個 Map,其中包含每個用戶的存盤庫。所以因此我認為repositoriesUserData應該是一個Map<String, List<String>.
考慮到這一點,讓我們在每個回圈周期中使用串列中的用戶作為鍵和存盤庫名稱串列作為值填充映射。
該方法看起來像這樣(洗掉了臨時回傳并將其替換為 void)
public static String repositoriesUnderUser() throws IOException {
for (int i = 0; i < fillList().size(); i ) {
GHUser user = githubLogin.getUser(fillList().get(i));
Map<String, GHRepository> temp = user.getRepositories();
repositoriesUserData.put(user.getLogin(), temp.values().stream().map(GHRepository::getName).collect(Collectors.toList()));
}
return "temporaryReturn";
}
編輯:(簡短說明您的代碼中發生了什么)
- 您正在將所有用戶名收集到本地串列
names,并將所有存盤庫名稱添加到本地串列“repoNames”。 - 在該方法結束時,您將一個新條目添加到您的地圖中
repositoriesUserData。
這意味著在該方法的末尾,您只向地圖添加了一個條目,其中
- 鍵 = 所有用戶
- value = 來自用戶的所有存盤庫(因為它是一個串列,如果兩個用戶具有相同的存盤庫,則將它們兩次添加到此串列中)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/366975.html
