1
Hashtable<String, Integer> coordinates= new Hashtable<String, Integer>();
String value = (String) cb1.getSelectedItem();
if(value==coordinates.keys())

It gives me a "imcompatible operand types String and Enumeration" error, any idea how I can compare these two without a loop?

Commongrate
  • 123
  • 1
  • 12

2 Answers2

3

You can't simply because a Set is a collection of Strings. Comparing would be like "I've got this bucket of apples and I got this apple, let's compare them". What you can do though is check whether the String is part of the Set, which is as simple as calling coordinates.keys().contains(value).

And regarding the comparison:
Don't compare Strings using ==. == compares by value, which would in case of Objects such as Strings be the reference. Use string1.equals(string2) instead. This topic is covered more extensively in this question

Community
  • 1
  • 1
0

Use the keySet method which will return a Collection. Then call contains on it with the selected value.

    Hashtable<String, Integer> coordinates= new Hashtable<String, Integer>();
    String value = (String) cb1.getSelectedItem();
    if (coordinates.keySet().contains(value)) {
        //Some action...
    }
Nick
  • 114
  • 5