Using HashMaps

Discussion in 'Plugin Development' started by marshmallowz, Oct 27, 2013.

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

    marshmallowz

    Hi I'm having trouble with using hashmaps they're kind of confusing for me, but what I'm trying to do is when someone uses my command I want to add them to a hashmap + however many times they used the command. Help is very appreciated :)
     
  2. Offline

    alex123099

    marshmallowz Why do you need the hashmap? What are you planning on doing with it?
     
  3. Offline

    marshmallowz

    I want to count how much times they used the command.
     
  4. Offline

    vemacs

    A HashTable might be more suitable.

    Code:java
    1. private static Hashtable<String, Integer> timesRun = new Hashtable<String, Integer>();


    to put a value in:

    Code:java
    1. if (timesRun.contains(player.getName()))
    2. timesRun.put(player.getName, timesRun.get(player.getName()) + 1);
    3. else
    4. timesRun.put(player.getName, 0);
    5.  


    to get a value:

    Code:java
    1. timesRun.get(player.getName());


    Wrote this off the top of my head, copy-pasting it might not work. Use your head.
     
  5. Offline

    alex123099

    marshmallowz
    I'm not sure if this is the most efficient way, but since you are storing 3 different variables I'd put a hashmap in a hashmap:
    HashMap<Integer, HashMap<String, String>> cmdsCounter = new HashMap<Integer, HashMap<String, String>>();
    The integer will be the number of times the command was executed, the inside hashmap will store the command and the player name. Or alternatively you could make the inside hashmap have String and Player or Player and String.
     
Thread Status:
Not open for further replies.

Share This Page