开发者

Java Map did not accept "boolean" [duplicate]

This question already has answers here: Why don't Java Generics support primitive types? (5 answers) Closed 8 y开发者_运维问答ears ago.

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 booleans (primitive). Look up autoboxing for explanation why this works. There are some pitfalls of using autoboxing but in simple cases it should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜