HashMap<Player,Boolean> vs ArrayList<Player>

Discussion in 'Plugin Development' started by SgtStud, Mar 24, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    SgtStud

    I am currently developing a plugin that modifies the players movement. I need to check whether or not the player is using the plugin when they type in a certain command.

    Would it be smarter to use a HashMap<Player,Boolean> and check whether they are using the plugin (true), or check if they are in an ArrayList?

    Right now im thinking that an ArrayList would take up less space, but what do you guys think? Is it worth it to use a hashMap?
     
  2. No, use a List/ArrayList/HashSet instead.

    Also, store player name, using String class, not the Player class.
     
  3. Offline

    SgtStud

    Ok thanks for the input. Also, why is it better to store it as a String?
     
  4. Offline

    comp8nerd4u2

    There is nothing wrong with using a HashMap. Indeed you are mapping a variable to a player, but use player names (String) instead of Player. It's more of an aesthetic and coding style kind of thing. What approach appeals more to you and more importantly, does it achieve the desired result? Anyone who tells you that you MUST do it their way even though that other ways exist probably has good reason, BUT early optimization is evil :) Do whatever it takes to get it working first, and THEN you may start optimizing. Else, you will have a very nasty headache as you are not letting yourself code freely and being forced to think carefully all the time, which is not good since you are still in the design phase.

    EDIT: Use Strings because they are immutable and Players are not, and the Player object is unreliable for comparision so it wouldn't work well in the Java Collections objects (HashMap) as a key
     
    SgtStud likes this.
  5. Offline

    SgtStud

    Ok thanks for the input!
     
  6. Offline

    Sorrow

    I think the best way to store that kind of data is a Set. It doesn't matter in which position the player is, right? The only thing you want to know is if the player is or not in that collection? The Set should be the best choice (you should use HashSet since Players are not comparable, I think).
    It stores a collection of sigle objects (does not accept duplicates) with no index, so you can only add, remove and check if an object is in the set. You can't get an object in the set, I think.
    The fact that it is specialized only to check if an object is in the list, it should do it very fast compared to other data structures.

    http://docs.oracle.com/javase/6/docs/api/java/util/Set.html
     
    desht likes this.
Thread Status:
Not open for further replies.

Share This Page