[UNSOLVED]Remove Players From HashMap With Specified Value Only

Discussion in 'Plugin Development' started by leimekiller, Oct 11, 2013.

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

    leimekiller

    Hello,

    I have a HashMap wich conains a Player and a String, I want to remove all players with a specified value only. So for example when I type /reset it would remove all players from the HashMap with the value X Only.

    Thanks in advance!
     
  2. Code:java
    1. For (int I = 0; I < yourhashmap.size(); i++){
    2. if (yourhashmap.get(i).contains("X")){
    3. yourhashmap.remove(I);
    4. }
    5. }

    didn't test this, but maybe it works
    leimekiller
     
  3. Offline

    leimekiller

  4. Offline

    Sour

    leimekiller First off, it's not a good idea to store Player objects. It's better to store their name and then get the object of a player from their name. Otherwise you'll have memory leak issues.

    However, since I'm answering your question, here is the code.
    Replace "hashMap" with your HashMap object.
    Code:java
    1.  
    2. for(Player player : hashMap.keySet()) {
    3. if(hashMap.get(player).equals("value")) {
    4. hashMap.remove(player);
    5. }
    6. }
    7.  
     
  5. Offline

    1Rogue



    If you really want to be picky, since a HashMap can store null values, you can use:

    Code:java
    1. for(Player player : hashMap.keySet()) {
    2. if("value".equals(hashMap.get(player))) {
    3. hashMap.remove(player);
    4. }
    5. }


    To avoid any potential NPE's.
     
Thread Status:
Not open for further replies.

Share This Page