Java Map did not accept "boolean" [duplicate]
Maybe is a newbie question, but I don't understand why when I try to do something like Map<String, boolean>
my IDE screams saying "Syntax error on token "boolean", Dimensions expected after this token", but with Boolean it works perfect. Can anyone explain me why is it like that? Thanks in advance!!
Simply put: Java generics don't work with primitive type arguments, only classes. So in the same way, you can't use List<int>
, only List<Integer>
.
See the relevant Java Generics FAQ entry for more information.
Use Boolean instead of boolean. Map can only contain objects and boolean is a primitive type not an object. Boolean is object wrapper of boolean.
In addition to other responses, note that you can use Map<String, Boolean>
and use them almost as if it were Map<String, boolean>
. That is, you will be able to put
and get
boolean
s (primitive). Look up autoboxing for explanation why this works. There are some pitfalls of using autoboxing but in simple cases it should work.
精彩评论