HashMap Help!

Discussion in 'Plugin Development' started by Gingerbreadman, Oct 29, 2014.

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

    Gingerbreadman

    Hello everyone!
    I am stuck on some HashMap work on my plugin,
    I am trying to check if anyone has a value of 0 in the HashMap
    if yes they will be kicked out of it!
    btw this is in an event because I want it to subtract 1 from the player every time the event fires
    Code:java
    1. if(map.containsKey(((Player) shooter).getName())) {
    2. for(Entry<String, Integer> entry : map.entrySet()) {
    3. if(entry.getValue().equals(0)) {
    4. map.remove(entry.getKey());
    5. }
    6. }
    7. map.put(((Player) shooter).getName(), map.get(((Player) shooter).getName()) - 1);
    8. }
     
  2. Offline

    guitargun

    on the line:
    Code:java
    1. map.put(((Player) shooter).getName(), Minebow.get(((Player) shooter).getName()) - 1);

    wouldn't the kicked out player be in the map again?
     
  3. Offline

    fireblast709

    Gingerbreadman you can't remove elements whilst iterating over the Map.
     
  4. fireblast709 He doesn't iterate over the Map though, does he? He's iterating over the entry set. :p
     
  5. Offline

    Lolmewn

    AdamQpzm Almost the same thing :p He's going to have to use an iterator, instead.
     
  6. Offline

    Abs0rbed

    I don't even think you need the for loop because hashmaps have key/value pairs. There will always be only one value associated with only that name. So just setting it again will wipe out the value.
    (This is assuming I understand what you're trying to do with your code, let me know if I got the wrong idea.)
    Code:java
    1. if(map.containsKey(((Player) shooter).getName())) {
    2.  
    3. if(map.get(((Player)shooter).getName()) == 0){
    4.  
    5. map.put(((Player) shooter).getName(), Minebow.get(((Player) shooter).getName()) - 1);
    6.  
    7. }
    8.  
    9. }

    Also, you were using .equals to compare to a number, which you usually don't do (unless there's something in the Entry class that I don't get.
     
  7. Offline

    Gingerbreadman

    Abs0rbed Lolmewn AdamQpzm fireblast709 I am trying to do this
    [Player, 10] - This is an example of someone in the Hashmap
    I want it to check if that player [shooter] has more than 1 in the HashMap
    Then if they do it will subtract one else they get removed from list
     
  8. Offline

    Lolmewn

    Gingerbreadman
    So basically like this:
    Code:
    if(map.containsKey(player)){
        map.put(player, map.get(player) -1);
        if(map.get(player) == 0){
            map.remove(player);
        }
    }
    
    Or something?
     
  9. Offline

    Abs0rbed

    That makes sense. You can just use a simple if-else to check what the value is, then perform changes accordingly

    Code:java
    1. if(map.containsKey(((Player) shooter).getName())) {
    2.  
    3. if(map.get(((Player)shooter).getName()) < 0){
    4.  
    5. map.put(((Player) shooter).getName(), Minebow.get(((Player) shooter).getName()) - 1);
    6.  
    7. }else{
    8.  
    9. map.remove(((Player)shooter).getName());
    10.  
    11. }
    12.  
    13. }
     
  10. Offline

    Gingerbreadman

Thread Status:
Not open for further replies.

Share This Page