简体   繁体   English

将列表分组和排序到 Map

[英]Grouping and sorting Lists into a Map

Before anything, the title doesn't convey what I really want to ask.首先,标题并没有传达我真正想问的内容。 What I want to know is, how can I make a map, where for several users, it collects their Data and then groups it all together.我想知道的是,我怎样才能制作一个 map,对于几个用户,它会收集他们的数据,然后将它们组合在一起。 I'm currently using two lists, one for the users' names and another for their works.我目前正在使用两个列表,一个用于用户名,另一个用于他们的作品。 I tried using a map.put but it kept overwriting the previous entry.我尝试使用 map.put 但它一直覆盖上一个条目。 So what I'd like to obtain is as follows;所以我想获得如下;

Desired output:所需的 output:

{user1 = work1, work2, work3 , user2 = work1, work2 , userN = workN} 

Current output:当前 output:

{[user1, user2, user3, user4]=[work1, work2, work3, work4, work5  (user1) , work1 (user2), work1, work2, work3 ( user3 )]}

This is the code that I'm currently using to achieve the above.这是我目前用来实现上述目标的代码。

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";
}

All help is appreciated!感谢所有帮助!

I'll give it a try (code in question still not working for me):我会试一试(有问题的代码仍然不适合我):

If I understood correctly, you want a Map, that contains the repositories for each user.如果我理解正确,您需要一个 Map,其中包含每个用户的存储库。 So therefore i think the repositoriesUserData should be a Map<String, List<String> .因此,我认为repositoriesUserData应该是Map<String, List<String>

With that in mind, lets fill the map in each loop-cycle with the user from the lists as key and the list of repository-names as value.考虑到这一点,让我们在每个循环周期中填充 map,列表中的用户作为键,存储库名称列表作为值。

The method would look like this (removed the temporary return and replaced it with void)该方法看起来像这样(删除临时返回并用 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";
  }

Edit: (Short explanation what is happening in your code)编辑:(简短说明您的代码中发生了什么)

  • You are collecting all usernames to the local List names and also adding all repository-names to the local List 'repoNames'.您正在将所有用户名收集到本地列表names ,并将所有存储库名称添加到本地列表“repoNames”。
  • At the end of the method you put a new entry to your map repositoriesUserData .在方法结束时,您在 map repositoriesUserData中添加一个新条目。

That means at the end of the method you just added one single entry to the map where这意味着在方法结束时,您只需向 map 添加一个条目,其中

  • key = all of the users key = 所有用户
  • value = all of the repositories from the users (because its a list, if two users have the same repository, they are added twice to this list) value = 来自用户的所有存储库(因为它是一个列表,如果两个用户具有相同的存储库,则将他们两次添加到此列表中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM