Should I use a HashMap or an Arraylist for this?

Discussion in 'Spigot Plugin Development' started by jordanzilla_, Jan 21, 2014.

  1. I have been working on a minigame and it is going to have teams in it and I am trying to design the teams functionality. I have tried both ways but cannot seem to find a way to store the player inside the team. Here is my code should I use a hashmap or an arraylist and how would I add a player to it?

    Code (Text):
    package me.jordanzilla02.minigametrial;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class Teams {

        //Where the hashmap or arraylist should be

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
                Player p = (Player) sender;

                Player p = (Player) sender;


  2. YoFuzzy3


    If you're only storing player names in teams then maybe this:
    Code (Text):
    // Java 7
    // Key = team name
    // Value = players on team
    Map<String, List<String>> teams = new HashMap<>();
    • Informative Informative x 2
  3. Thank you
  4. There are 2 diff approaches I could/would take.

    Use a Hashmap to store everything in one object, using the Key as the player, the value as their team..

    Code (Text):
    HashMap<String, String> teams = new blah blah blah
    Or, you can have a separate object for each team,

    Code (Text):
    List<String> red = new ArrayList blah blah
    List<String> blue = new ArrayList anfnfdsfkds
    access it with If(red.contains(User) ) {}
  5. Thanks, I believe the Arraylist is slightly simpler than the HashMap, so I most like am going to go with the arraylist
  6. YoFuzzy3


    For two teams it might be, but always consider flexibility for future changes to a project. ;)
    • Agree Agree x 1
  7. Yeah, but for just some simple teams that I want to have, an ArrayList will probably be the easiest to work with.
  8. Tux



    Map<K, List<V>> can be a weird affair. You should use the Multimaps from Google Guava.
  9. I would create a class called Team which contains a List of players, an enum for the Color, etc...
  10. YoFuzzy3


    No point if he's only storing the list of players.
    • Agree Agree x 1
  11. But, are you having a class for "Arena" and such? @jordanzilla02
  12. Yes
  13. I personally have a Team object with a players list in it, and its name abd such. I then have a list of teams.
  14. Do you run it in your main class or its own personal class?
  15. I would use this:

    ArrayList<Player> red=ArrayList<Player>;

    to get a certain player from a team, I would do this:

    for(int i=0;i>=red.getSize();i++){
    if(player == red.get(i)){
    //do something to the player

    Or yeah, make a team class, have it abstract, put a few methods like: getPlayer(string playerName);
  16. Tux

    • Agree Agree x 3
  17. Ok
    • Optimistic Optimistic x 1