Solved HashMap question?

Discussion in 'Plugin Development' started by Gonmarte, Mar 18, 2016.

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

    Gonmarte

    Hello,
    I have a hashmap. I can easly get all the keys from that hashmap looping through it, but my question is if i can get all the keys from the hashmap that are associate to a specific value.
    Ty
     
  2. Offline

    Zombie_Striker

    @Gonmarte
    Loop through the hashmap's keys, get the values, and test if the values are equal to the 'specific value'. If so, add them to a new collection/array.
     
  3. Offline

    Gonmarte

    @Zombie_Striker
    It doesnt who up anything in console. I want to get all the keys associate to a value in this case - look below - i would like to get all the keys associate to the value IvD.
    This was the far as i got, what i need to do next?
    Code:
        for (Map.Entry<String, String> e : myMap.entrySet()) {
               String key = e.getKey();
               String value = e.getValue();
               System.out.println(key + value);
            }
    
     
  4. Offline

    Zombie_Striker

    @Gonmarte
    Test of "value" is equal to the 'specific value' you wanted. If so, store that key somewhere.
     
    Gonmarte likes this.
  5. Offline

    Gonmarte

    @Zombie_Striker
    Thank you, i solved :D
    Code:
            for (Map.Entry<String, String> e : myMap.entrySet()) {
                String value = e.getValue();
               if("IvD".equals(value)){
                  String key = e.getKey();
                  
                   System.out.println(key);
               }
            }
    
    [/QUOTE]
     
  6. Offline

    mcdorli

    [/QUOTE]
    Why do you do "IvD".equals(value)? Variables should come first, then the constants
     
Thread Status:
Not open for further replies.

Share This Page