是否可以在 Java 中使用原始类型 (int) 作为泛型类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/414408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:17:34  来源:igfitidea点击:

Is it possible to use a primitive type (int) in as a generic type in Java?

javagenericstypesprimitive

提问by Alexander

Specifically, with a SortedMap<Vector<String>, int>I get "dimensions expected after this (int) token." Help!

具体来说,SortedMap<Vector<String>, int>我得到“这个 ( int) 标记之后的预期尺寸”。帮助!

回答by Konrad Rudolph

No, this is not possible. Use Integerinstead. Autoboxing takes care of the rest (i.e. for most purposes you can program as if you had actually used intbecause Java converts to and from Integerautomatically for you).

不,这是不可能的。使用Integer来代替。自动装箱负责其余的工作(即,在大多数情况下,您可以像实际使用过一样进行编程,int因为 Java 会Integer自动为您进行转换)。

回答by Craig P. Motlin

Konrad is correct. Alternately, you can use the trove class TObjectIntHashMap to map Objects to primitive ints.

康拉德是对的。或者,您可以使用 trove 类 TObjectIntHashMap 将对象映射到原始整数。