Solved A question about HashMaps

Discussion in 'Plugin Development' started by derin38, Dec 28, 2012.

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

    derin38

    Hi Bukkit.

    I'm currently into developing my first plugin and I wonder if you could help me with kind of 'looping' through HashMaps.

    I currently have a HashMap consisting of the key as an Integer and a String as the value. When a player types a specific command the argument he typed will be set as the value of the next available integer (as the key). Therefore, I want to have some sort of loop, when the command-executor is run, that loops through the HashMap and reports, or chooses, the next available Integer that is not 'occupied' as any key.

    For example:

    My HashMap consists of key '1' and its value. When the command is run, I don't want to put the argument of the command to a specific key but to key '2' because key '1' is occupied.

    If my HashMap consists of key '1' and '2' I want to put the argument to key '2' because it's the next available.

    If my HashMap consists of key '1' '2' '3' and '5' I want to put the argument to key '4' because it's the next available.

    I do not need help with putting the argument of the command to the HashMap, only how to loop through the HashMap until the next available Integer is found.

    I was thinking that maybe I could use a loop that does ++ until containsKey is null, then reports back that Integer in an int that later on can be used to set the key.

    I hope you understand what I mean and can help me. Thanks.
     
  2. Offline

    caseif

    You could use HashMap.size(). If you start your keys at 0, you could just use this value as the next available key, but if you start at 1, you'll need to add one to it.
     
  3. Offline

    kellerkindt

    Code:
                        // initialize the HashMap
                        HashMap<Integer, Object> map = new HashMap<Integer, Object>();
                       
                        // iterate through the keys
                        for (Integer key : map.keySet()) {
                            Object value = map.get(key);
                           // ...
                        }
                       
                        // iterate through the values
                        for (Object value : map.values()) {
                           // ...
                        }
                       
                        // iterate through the entries
                        for (Entry<Integer, Object> entry : map.entrySet()) {
                            Integer key    = entry.getKey();
                            Object    value    = entry.getValue();
                           // ...
                        }
    But be aware, that a List (ArrayList) is a lot faster, \\
    if you want to iterate. If you are using Numbers from \\
    0-X, the ArrayList could be what you need.

    Code:
                        // initialize the HashMap
                        List<Object> list = new ArrayList<Object>();
                       
                        // add some values
                        list.add(new Object());
                        // ...
                       
                        for (Object value : list) {
                            // ...
                        }
                       
                        for (int i = 0; i < list.size(); i++) {
                            Object value = list.get(i);
                        }
     
  4. Offline

    derin38

    But this doesn't work right? :
    kellerkindt, you don't actually explain how I should make the iteration.

    I'm currently trying a for loop and a while loop within the for loop. The while loop is supposed to ++ until the key is null. But right now I don't know how to indicate the 'nullness' of the key.
     
  5. Offline

    caseif

    Alright, attempt number 2. Maybe create a new method, put a try/catch block inside of a for loop, try to get the mapped value of an incrementing integer, and then return that integer inside of the catch block.
     
  6. Offline

    kellerkindt

    Do you mean this one?

    Code:
    HashMap<Integer, String> map = new HashMap<Integer, String>();
     
    for (int i = 0; i < map.size(); i++) {
       String value = map.get(i);
     
       if (value == null) {
          // .. key is free
       }
    }
     
  7. Offline

    derin38

    Could you please explain? And possibly write a code as an example?

    I don't want to do anything with the String. I only want to increment the Integer until the key is null so that I know what Integer I can use.
     
  8. Offline

    caseif

    See kellerkindt 's post. The only purpose for the String variable is to serve as a test to check if the value has already been used in the HashMap. If it hasn't, it will return null, and you can set an int defined outside of the loop to i, and break the loop. If you're not mapping the keys to Strings, just replace the variable type with whatever you are mapping to.
     
  9. Offline

    derin38

    I don't know if I configurated it wrong but when i is not incrementing but the size of the map is.

    Code:
                for (int i = 0; i < MainClass.map.size(); i++) {
                  String value = MainClass.map.get(i);
                  if (value == null) {
                      sender.sendMessage("Value is: " + value + ". I is: " + i);
                  }
                      sender.sendMessage("(Outside if)Value is: " + value + ". I is: " + i + ". map size: " + MainClass.map.size());
                }    
    The code is inside of a CommandExecutor and the map is defined in the main class. (Not actually named MainClass)
     
  10. Offline

    fireblast709

    Should you not do something with the first free spot and break the loop
     
  11. Offline

    caseif

    NOO, don't do that. If you try to echo out the value, it will throw a NPE. I would do:
    Code:java
    1. int key = -1;
    2. for (int i = 0; i <= MainClass.map.size(); i++){
    3. String value = MainClass.map.get(i);
    4. if (value == null) {
    5. key = i;
    6. break;
    7. }
    8. }
    9. // do whatever you need to with key
     
  12. Offline

    derin38

    Many thanks to you sir, it worked!
     
  13. Offline

    caseif

    Glad to help. :3
     
    derin38 likes this.
Thread Status:
Not open for further replies.

Share This Page