Home > Mobile >  Grouping and sorting Lists into a Map
Grouping and sorting Lists into a Map

Time:11-27

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. 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. So what I'd like to obtain is as follows;

Desired output:

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

Current 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!

CodePudding user response:

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. So therefore i think the repositoriesUserData should be a 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.

The method would look like this (removed the temporary return and replaced it with 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'.
  • At the end of the method you put a new entry to your map repositoriesUserData.

That means at the end of the method you just added one single entry to the map where

  • key = all of the users
  • 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)
  • Related