Count arraymap matches

Discussion in 'Plugin Development' started by Zenok, Dec 31, 2013.

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

    Zenok

    Code:java
    1. Map<String, Integer> Zombies = new HashMap<String, Integer>();
    2.  
    3. Zombies.put(zombie.getUniqueId().toString(), game);



    I store the id of the zombies in arraymap, along with the id of the game.

    It is possible to count the number of elements in a arraymap from the id of the game and not the id of the zombie?

    I tried with Zombies.containsValue(game), but this only returns true
     
  2. Zenok
    ArrayMap?

    Anyways, do you mean how to count all the keys that have a same, specific game value?
     
  3. Offline

    TheKomputerKing

    Swap the parameters in the HashMap and use a for loop to loop through the keys, get their corresponding values and compare them to the game ID in question.
     
  4. TheKomputerKing
    That's what I would do too, seems kind of silly how he isn't using the game ids as keys, but whatever. To do the opposite, something like this should work.
    Code:java
    1. public int getCount(HashMap <String, Object> map, Object id) {
    2. int count = 0;
    3.  
    4. for (Map.Entry <String, Object> entry: map.entrySet()) {
    5. if (entry.getValue() == id) {
    6. count++;
    7. }
    8. }
    9.  
    10. return count;
    11. }
    This would count all the values that match the given object (id), then return how many there were.
     
    TheKomputerKing likes this.
  5. Offline

    Zenok

    I have the array that way, because they also use it in other functions to get the id of heading from the id of the entity.
     
Thread Status:
Not open for further replies.

Share This Page