HashMap

Discussion in 'Plugin Development' started by ProStriker123, Jul 22, 2014.

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

    ProStriker123

    Can someone give me a link thats explains whats its HashMap?
     
  2. Offline

    artish1

  3. Offline

    stormneo7

    Basically it is an ArrayList that stores two types of objects.
    For instance, say that I wanted to make a killstreak plugin.
    I would make a Hashmap that has a String and an Integer like so.
    Code:
    HashMap<String, Integer> killStreak = new HashMap<String, Integer>();
    Now I can store a PlayerName and their KillStreak by doing...
    Code:
    int killStreakNumber = 3;
    killStreak.put(p.getName(), killStreakNumber);
    There are a variety of things you can store in a HashMap. Here are a couple of examples...
    Code:
    HashMap<String, String> kitName = new HashMap<String, String>();
    kitName.put(p.getName(), "PVPKit");
     
    HashMap<String, List<String>> teamList = new HashMap<String, List<String>>();
    etc.
    
    Here is another example of what you could do.
    Code:
    HashMap<String, String> kitName = new HashMap<String, String>();
    //[HashMap].keySet();
     
    for(String s : kitName.keySet()){
      System.out.println(s + " is the " + kitName.get(s) + " kit.");
    }
    This example shows that .keySet() will return the first object (in a List) in the HashMap which in that case, we used to store the PlayerName. I've created a loop for the list.
    Doing kitName.get(s) [Their Name] will return their kitName which is our second object.

    If you have any questions, I'd love to answer them.
     
    Dragonphase and xmarinusx like this.
  4. Offline

    xmarinusx

    stormneo7
    Good explanation. However use the player's UUID instead of the player's name ;)
     
  5. Offline

    fireblast709

    xmarinusx just as an informational sidenote: you can use player names if they are only kept within the scope of login and quit, and not for persistence.
     
    Dragonphase and xmarinusx like this.
  6. Offline

    Double0negative

    In which case you might as well use a Player object
     
    Zupsub likes this.
  7. Offline

    ProStriker123

  8. Offline

    Dragonphase


    As long as you handle the data correctly to avoid possible memory leaks.
     
  9. Offline

    _Filip

    fireblast709 Bukkit will soon put player objects in a hashmap, instead of a List, with the UUID as the key. So it will be more efficient storing the UUID if you plan on doing Bukkit#getPlayer().
     
  10. Offline

    1Rogue Retired Staff


    Fairly certain they've been in a map for a while, just indexed by name.

    Edit: I'm wrong
     
    TheSpherret likes this.
Thread Status:
Not open for further replies.

Share This Page