0

I wana compare a String with the Key of a Hashmap.

So every time i run this code it uotputs : Not found

I'm new at Java and its surely a little thing but i need help.

Here my code

btnNewButton.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) 
        {
            if(!txtSearchHere.getText().isEmpty() && txtSearchHere.getText().length() > 1)
            {
                String value = txtSearchHere.getText();
                txtSearchHere.setText("");

                for(Integer key : plzHashMap.keySet())
                {
                    if(key.toString() == value)
                    {
                        System.out.println("Matched key = " + value);
                    }
                    else
                    {
                        System.out.println("Not found");
                    }
                }
            }
        }
    });
N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
Alex
  • 491
  • 2
  • 10
  • 25
  • 1
    Use `key.toString().equals(value)` as `==` will compare if it is the same object (reference) whereas `.equals()` compares the values. – LordAnomander Feb 12 '16 at 08:06
  • Try key.toString().equals(value) – anaxin Feb 12 '16 at 08:08
  • I think your logic is wrong. Are you sure that you want the USER to enter a KEY? Based on your "txtSearch" variable, I assume you are not intended to do that. – Peter Feb 12 '16 at 08:15

4 Answers4

1

For all String-comparisons in Java you should use .equals() instead of ==.

So change:

if(key.toString() == value)

to:

if(key.toString().equals(value))

The reason for this is that == is used for checking if the instances are the exact same (reference), while .equals checks for the same value.

See this SO question (among a lot of others) for more info

Community
  • 1
  • 1
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
0

Try the following:

String.valueOf(key).equals(value)

And never compare String with ==, use .equals().

Peter
  • 323
  • 1
  • 16
  • 46
0

Firstly, compare strings using .equals(), not ==.

Secondly, it outputs Not found lots of times because that statement is inside the loop. You can move it outside the loop:

            boolean found = false;
            for(Integer key : plzHashMap.keySet())
            {
                if(key.toString().equals(value))
                {
                    System.out.println("Matched key = " + value);
                    found = true;
                    break;
                }
            }
            if (!found) {
                System.out.println("Not found");
            }

However, it would be a lot easier just to convert value to an Integer:

Integer intValue = Integer.parseInt(value);

and then just call get and hasKey - no looping required:

if (plzHashMap.hasKey(intValue)) {
  System.out.println("Matched key = " + plzHashMap.get(intValue));
} else {
  System.out.println("Not found");
}

Of course, you'd need to handle the case that value can't be parsed to an int.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

The == operator checks for reference identity. You should use the equals methods to check for equality:

if(key.toString().equals(value))

But regardless, you're using an O(n) iteration on a hashmap which was designed to provide O(1) key lookup - why not use it?

boolean found = false;
try {
    Integer integerValue = Integer.valueOf(value);
    if (plzHashMap.containsKey(integerValue)) {
        System.out.println("Matched key = " + value)
        found = true;
    }    
} catch (NumberFormatException ignore) {
    // value is not even a number
}

if (!found) {
    System.out.println("Not found");
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350