It will work if you used the primitive boolean
literals to add elements to your map.
When using primitive literals where a reference type is expected, the compiler will use Boolean#valueOf(..)
implemented as
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
to return a value of type Boolean
. So you will get the same two Boolean
objects every time.
If you instead added to the map with
new Boolean(true) // or new Boolean(false)
it will be a different reference, so comparison with ==
will fail.
This does seem like premature optimization.