Selzier
Selzier

Reputation: 101

Java HashMap Iterate Each Var and Return Value: Boolean boolean , Integer int error?

HashMap<String, Boolean> mAchBools = new HashMap<String, Boolean>(){{
    put("myBool1", false);
    }
};

HashMap<String, Integer> mAchInts = new HashMap<String, Integer>(){{
    put("myInt1", 0);
    }
};

boolean isEmpty() {
    for (boolean b : mAchBools.values()) if (b) return false;
    for (int i : mAchInts.values()) if (i != 0) return false;           
    return true;            
}

I create a HashMap will Booleans and a HashMap with Integers. Now I want a method to check and see if the values are "empty" (all booleans are false and all integers are 0).

I run Debugger and I can see that my boolean in the Hashmap is false, but my method "isEmpty" still returns false.

Why does my "isEmpty" check fail? I think it's a difference between boolean and Boolean but I'm not sure.

Upvotes: 0

Views: 489

Answers (1)

Error 454
Error 454

Reputation: 7315

Java automatically converts between Boolean <> boolean and int <> Integer. So your code is technically correct as written.

Upvotes: 1

Related Questions