2

I (mistakenly) wrote the following code in an Android project.

for (Map<String, Object> occurrence : occurrences) {
    if (occurrence.get("cancelled") == true) {
        // don't display cancelled events
        continue;
    }
    ...

Now, as I understand it, what should have happened there is a compiler error to the effect of error: incomparable types: Object and boolean, but that's not what happened. It compiled successfully, and behaved the way I had expected/intended. (It is true that the value that will be retrieved here will always be a boxed boolean.)

I never even would have noticed this, but some time after I submitted the code, a collaborator pulled to his system, tried to compile, and got a compile error.

After that happened I double checked to make sure, but my code is definitely using the == operator, and it still compiles fine even after cleaning and rebuilding.

What's going on here? Is there some setting to tell Android Studio to gloss over this? Do different versions of the java toolchain allow or disallow this syntax? I've got this particular example working by just replacing the comparison with a call to .equals, but we need to be sure in the future that when we write/compile/test code it won't randomly not work at all on the other guy's machine!

GrandOpener
  • 1,943
  • 1
  • 17
  • 25
  • Which OS, IDE, compiler version etc. were you both running? – shmosel Apr 28 '16 at 03:49
  • 1
    Here is a related question and answer that might help explain it. http://stackoverflow.com/questions/16770403/equality-comparison-of-boolean-and-object-allowed. – George Mulligan Apr 28 '16 at 03:50
  • @GeorgeMulligan Good find; I don't know why my searches didn't see that one. It looks like that other question does have the necessary leads to track this down, but the question is different enough to probably not be an exact duplicate (eclipse vs. android studio), and also the accepted answer is rather vague with the link having apparently rotted. If you want to write up the details of changing that setting in Android Studio I'll accept the answer, otherwise I'll probably get around to it myself this weekend. – GrandOpener Apr 29 '16 at 02:33
  • Thanks for giving me the chance. You can go ahead and write up the answer this weekend or whenever you get around to it. – George Mulligan Apr 29 '16 at 02:41

1 Answers1

0

Map will hold key and value pair. In your code you defined Map that means, key is a String type and corresponding key your saving value as a object not boolean.

If you want to check key is present in map then you have to use map.containsKey(key). This method will returns true if key is present in the hashmap. If you want to compare the current element is present then you have to use get(), This method will returns the Value type.

So final conclusion your code is:

for (Map<String, Object> occurrence : occurrences) {
    if (occurrence.get("cancelled")!=null) {
        // don't display cancelled events
        continue;
    }

Method 2:

for (Map<String, Object> occurrence : occurrences) {
    if (occurrence.containsKey("cancelled")) {
        // don't display cancelled events
        continue;
    }
Jitendra Kumar. Balla
  • 1,173
  • 1
  • 9
  • 15