Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Soccer League Organizer in Java

Steve Fielding
Steve Fielding
7,287 Points

Creating Multiple list with HashMap or MultiMap

I am trying to create a multiple list of players in teams but i am having some difficulty. I want to add the players "blockplayer" to Teams "displayTeam", which contains a list of teams, i can choose to add a player to. I tried it with the guava library "MultiMap" but it still did not store my list.

case "transfer":
                        String displayTeam = promptForTeam();
                        blockPlayer = new ArrayBlockingQueue<String>(MAX_NUM);
                        Player toAddPlayer = tPlayers.poll();
                        blockPlayer.add(toAddPlayer.getLastName()+" "+toAddPlayer.getFirstName());
                        Map<String, BlockingQueue<String>> addBlock = new HashMap<>();
                        addBlock.put(displayTeam, blockPlayer);
                        for (BlockingQueue<String> entry : addBlock.values()) {
                            String displayPlayer =  entry.peek();
                            List<String> addMehn = new ArrayList<>();
                            addMehn.add(displayPlayer);
                            System.out.printf("%s %n%n", addMehn);
                        }
Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Could you push your project on GitHub, so that it is possible to take a look and play with it a bit....

Also could you comment the lines that do not work...

All I can infer is that

addBlock.put(displayTeam, blockPlayer)

does not work, right ?

Is there any error, or just addBlock list is empty ?

Anyway if you can put comments how your code works, and to which exactly what was not added, it could be helpful, e.g.

                        String displayTeam = promptForTeam();
                        blockPlayer = new ArrayBlockingQueue<String>(MAX_NUM);
                        Player toAddPlayer = tPlayers.poll();
                        blockPlayer.add(toAddPlayer.getLastName()+" "+toAddPlayer.getFirstName());
                        Map<String, BlockingQueue<String>> addBlock = new HashMap<>();
                        // adding blockPlayer here
                        addBlock.put(displayTeam, blockPlayer);
                       // but if I print addBlock here it prints null , or something like that.
                       // the faster I know what is exactly is not filled, the faster error
                       // could be found
                       addBlock.get(displayTeam);
                        for (BlockingQueue<String> entry : addBlock.values()) {
                            String displayPlayer =  entry.peek();
                            List<String> addMehn = new ArrayList<>();
                            addMehn.add(displayPlayer);
                            System.out.printf("%s %n%n", addMehn);

                        }

3 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

I left a comment on your issue in GitHub.

But I'll duplicate here just in case:

Again it is very hard to understand why do you use addBlock ?

All I could infer that If you need to create HashMap where for each String teamName there will be ArrayBlockingQueue<String> with 11 Players names, like this:

"Team 1" -> " 'Bob Smith', 'John Doe' ..." ,
"Team 2" -> " 'Alex Smith', 'John Smith' ..." ,

Then this should work:

I. I added addBlock HashMap as member for it to be re-filled

And that is what I added in constructor and to members ...

    private Map<String, BlockingQueue<String>> addBlock;


    public Menu(League league) {
        //Menu for application
        this.league = league;
        tReader = new BufferedReader(new InputStreamReader(System.in));
        tPlayers = new ArrayDeque<>();
        tMenu = new LinkedHashMap<>();
        addBlock = new HashMap<>();
        tMenu.put("Add ", "Add a new team and coach to the League");
        tMenu.put("Remove", "Remove Player from team");
        tMenu.put("transfer", "Add Player to Team");
        tMenu.put("Team", "Print a List of Teams");
        tMenu.put("Player", "Print a List of Players and select player");
        tMenu.put("Quit", "Give up. Exiting Program.....");
    }

II. In your transfer part, I

  • get BlockingQueue of Player Names, represented as String.
  • if it is null, then I create new ArrayBlockingQueue with players for Team
  • then I put the players polled to the HashMap, checking also that players were not added before
                    case "transfer":
                        /*
                        Problem area. Need Fixtures
                        After add Teams and selecting a player to add, this code should be able to store players in teams
                        ie: use displayTeam as key and blockPlayers as values
                        Bug: Code is only able to store one value in the HashMap. All efforts to store addition values in the
                        HashMap deletes the right value and replaces it with the new value.
                        Use MultiMap from guava also produced the same function.
                        Multimap<String, BlockingQueue<String>> addBlock = ArrayListMultimap.create();
                        */
                        String displayTeam = promptForTeam();

                        // get the display team from HashMap
                        BlockingQueue<String> players =
                                addBlock.get(displayTeam);

                        boolean noPlayersWereAddedBefore = false;
                        if (players == null) {
                            players = new ArrayBlockingQueue<String>(11);
                            noPlayersWereAddedBefore = true;
                        }
//                        blockPlayer = new ArrayBlockingQueue<String>(MAX_NUM);

                        Player toAddPlayer = tPlayers.poll();

                        players.add(toAddPlayer.getLastName()+" "+toAddPlayer.getFirstName());

//                        addBlock.put(displayTeam, blockPlayer);
                        if (noPlayersWereAddedBefore) {
                            addBlock.put(displayTeam, players);
                        }

                        System.out.println(addBlock);
                        for (BlockingQueue<String> entry : addBlock.values()) {
                            System.out.printf("%s %n%n", entry.peek());
                        }

I would say although, that You are making this over too complicated.

Here is my suggestions how to changed Architecture and Design of the App.

I. Try to separate TeamChangePrompter with LeagueChangePrompter. You can create TeamChangePrompter, that will take Team as argument on constructor, change players inside and return back Team to work in a League. In LeagueChangePrompter, you can add Teams, and print their Stats.

Why do I ask you to do this, because of Single Responsibility Principle. Try to separate things as much as possible.

That's why I highly encourage you to create TeamChanger class that takes chosen Team, add players and stuff, and returns back changed Team, and LeagueChanger, where you can Create new teams, show parameters for teams, but not players.. You see what I mean ?

II. If you implement properly Step I, then you don't need to overwhelm your App with unecessary complex structures as HashMap<String, ArrayBlockingQueue<String>>. I was able to use only Set<Team> and Set<Player>.

Please take a look at my solution, may be that will encourage you, may be not .

https://github.com/nikiforov-alexander/pt2-soccer-league-organizer

Steve Fielding
Steve Fielding
7,287 Points

Hi Alexander and all, There is no error with adding the keys and values to the HashMap. My issue is that the HashMap code is only able to store one value in the HashMap. All efforts to store addition values in the HashMap deletes the first value and replaces it with the new value. I am thinking the only solution is to serialize the value to a file. My problem is on Menu.class number 92 - 108. This is the github link to the project Soccer Project

Steve Fielding
Steve Fielding
7,287 Points

Thanks @alexandernikiforov