1

I am having difficulties to compare the string (str) with the keys in hashmap. As you can see from the below code, I have multiple keys. I am comparing the str with the keys but I couldn't get the values for that specific key. Please note the map contains coin.

it works only if i directly input the string eg entry.getKey().getKeyOne().toString().contains("coin")

String str = "coin";
for(Map.Entry<Pair, String> entry : map.entrySet()) {
    if(entry.getKey().getKeyOne().toString().contains(str)||
        entry.getKey().getKeyTwo().toString().contains(str)) {
            entry.getValue();
    }
}

public class Pair {
    private String keyOne;
    private String keyTwo;

    Pair(String one,String two) {
        this.keyOne=one;
        this.keyTwo=two;
    }

    public String getKeyOne() {
        return keyOne;
    }

    public String getKeyTwo() {
       return keyTwo;
    }
}
cocoatomo
  • 5,432
  • 2
  • 14
  • 12
elyon
  • 37
  • 6

2 Answers2

1

Advice: As long you are using your own class Pair in a Map you must override equals() and should override hashCode().

You example with the string literal works, because "coin" is in the string pool and all references the same String.

Runs here:

public class Pair {

    private String keyOne;
    private String keyTwo;

    Pair(String one, String two) {
        this.keyOne = one;
        this.keyTwo = two;

    }

    public String getKeyOne() {
        return keyOne;
    }

    public String getKeyTwo() {
        return keyTwo;

    }

    public static void main(String[] args) {
        Map<Pair, String> map = new HashMap();
        map.put(new Pair("coin", "5"), "U");
        map.put(new Pair("bill", "100"), "H");
        map.put(new Pair("10", "5coin"), "T");

        String str = new String("coin");
        for (Map.Entry<Pair, String> entry : map.entrySet()) {
            if (entry.getKey().getKeyOne().toString().contains(str)
                    || entry.getKey().getKeyTwo().toString().contains(str)) {
                System.err.println(entry.getValue());
            }
        }
    }
}
Community
  • 1
  • 1
PeterMmm
  • 24,152
  • 13
  • 73
  • 111
0

Here is simple sample code for you If you want it for one Hashmap only

public void findInkeys(String findkey){
    HashMap<String, Object> map=new HashMap<>();

    //Place your content of MAp Here
    //Start

    //end
    //or you can place your content anywhere else but make sure is is initialized before this


    Set<String> keys=map.keySet();
    boolean isInKeys=false;
    for (String k : keys)
    {
        if(k.equals(findkey))
        {
            isInKeys=true;
        }
    }
    System.out.println(isInKeys);
}

if you want it for any hashmap

public void findInkeys(HashMap<String,Object> map,String findkey){

    Set<String> keys=map.keySet();
    boolean isInKeys=false;
    for (String k : keys)
    {
        if(k.equals(findkey))
        {
            isInKeys=true;
        }
    }
    System.out.println(isInKeys);
}

if you have multiple Keys to compare then go with below code

public void findInkeys(HashMap<String, String> map,ArrayList<String> keyList){
    Set<String> keys=map.keySet();
    boolean isInKeys=false;
    for (String k : keys)
    {
        if(keyList.contains(k))
        {
            isInKeys=true;
        }
    }
    System.out.println(isInKeys);
}
Shalin Patel
  • 1,079
  • 1
  • 10
  • 15