Checking how many objects in a HashMap have the same value

Discussion in 'Plugin Development' started by MCCoding, Jun 2, 2014.

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

    MCCoding

    I'm saving a two players into a HashMap but i'm not sure on how to check on how many values of those are the same, for example:

    Code:java
    1. map.put("x", "y");
    2. map.put("g", "y");
    3. map.put("e", "r");
    4. map.put("k", "y");


    So when i call my method getSameValues() which takes a value so if i did it getSameValues("y") it should return 3 as a int. But i have not clue on how i compare Hash Map Values with each other.
     
  2. Offline

    xTrollxDudex

  3. MCCoding
    Iterate through the map and increment an int if the values match up. For example:

    Code:java
    1. private HashMap<String, String> map = new HashMap<String, String>();
    2. private int valueCount;
    3.  
    4. public int getSameValues(String value){
    5. for (String s : map.values()){
    6. if (value.equals(map.get(s)))
    7. valueCount++;
    8. }
    9. return valueCount;
    10. }
     
  4. Offline

    MCCoding

    The Gaming Grunts

    Thanks for the help but when i try to get the value it returns 0. here is what i have

    Code:java
    1. player.sendMessage(getSameValues(player.getName()));


    and it returns 0, my value in my HashMap is MCCoding and the value i'm adding is MCCoding. the values it's spitting out when i add a print line to the for loop in getSaveValues are MCCoding, MCCoding, that23 so it should return 2 not 0, know why this may happen?
     
  5. Offline

    Not2EXceL

    The Gaming Grunts
    -_-
    map.get(s);
    You're iterating the values, why would you be getting anything? just value.equalsIgnoreCase(s);
     
    MCCoding likes this.
  6. The Gaming Grunts Yes, that absolutely needed to be a field, and nothing will go wrong with it like that. :p
     
  7. Offline

    MCCoding

  8. EDIT MCCoding Also, local variable, not field.
     
  9. Offline

    RawCode

    when you store objects, you also can store number of instances inside referse lookup table.
     
  10. Offline

    MCCoding

    AdamQpzm
    Ah thanks for that git it all working!
     
  11. MCCoding
    Sorry. There's also a .matches() that you could use. For example:

    Code:java
    1. private static HashMap<String, String> map = new HashMap<String, String>();
    2. private static int valueCount;
    3.  
    4. public static void main(String[] args){
    5. map.put("doge", "test");
    6. map.put("derp", "test");
    7. map.put("herp", "test1");
    8. map.put("hello", "bye");
    9. System.out.println(getSameValues("test"));
    10. }
    11.  
    12. public static int getSameValues(String value){
    13. for (String s : map.values()){
    14. if (value.matches(s))
    15. valueCount++;
    16. }
    17. return valueCount;
    18. }


    That will print out 2, as there are 2 values in the map that match "test".
     
  12. Offline

    RawCode

    Garris0n and AdamQpzm like this.
  13. The Gaming Grunts Every variable that could possibly be a field there, is a field! Amazing.
     
  14. Offline

    Not2EXceL

  15. Offline

    fireblast709

    Code:java
    1. Collections.frequency(map.values(), "needle")
    Anyone?
     
    NathanWolf likes this.
  16. Offline

    1Rogue


    If not that, it's a simple re-mapping:

    Code:java
    1. public <T> Map<T, Integer> getValueOccurances(Map<?, T> original) {
    2. Map<T, Integer> back = new HashMap<>();
    3. for (T val : original.values()) {
    4. if (back.containsKey(val)) {
    5. back.put(val, back.get(val) + 1);
    6. } else {
    7. back.put(val, 1);
    8. }
    9. }
    10. return back;
    11. }
     
    NathanWolf likes this.
Thread Status:
Not open for further replies.

Share This Page