Java 映射和原语
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4399645/
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
Java Mappings and Primitives
提问by James
I want to create a mapping that takes a String
as the key and a primitiveas the value. I was looking at the Java docs and did not see that Primitive was a class type, or that they shared some kind of wrapping class.
我想创建一个以 aString
为键和一个原语为值的映射。我正在查看 Java 文档,并没有看到 Primitive 是一种类类型,或者它们共享某种包装类。
How can I constrain the value to be a primitive?
如何将值限制为原始值?
Map<String, Primitive> map = new HashMap<String, Primitive>();
Map<String, Primitive> map = new HashMap<String, Primitive>();
回答by khachik
Java Autoboxingallows to create maps on Long, Integer, Double
and then operate them using primitive values. For example:
Java 自动装箱允许创建映射Long, Integer, Double
,然后使用原始值操作它们。例如:
java.util.HashMap<String, Integer> map = new java.util.HashMap<String, Integer>();
map.put("one", 1); // 1 is an integer, not an instance of Integer
If you want to store in one map different primitive types, you can to it by making a Map<String, Number>
. Allows to store values of BigDecimal
, BigInteger
, Byte
, Double
, Float
, Integer
, Long
, Short
(and AtomicLong
, AtomicInteger
).
如果你想在一个映射中存储不同的原始类型,你可以通过创建一个Map<String, Number>
. 允许存储BigDecimal
, BigInteger
, Byte
, Double
, Float
, Integer
, Long
, Short
(和AtomicLong
, AtomicInteger
) 的值。
Here is an example:
下面是一个例子:
Map<String, Number> map = new HashMap<String, Number>();
map.put("one", 1);
map.put("two", 2.0);
map.put("three", 1L);
for(String k:map.keySet()) {
Number v = map.get(k);
System.err.println(v + " is instance of " + v.getClass().getName() + ": " + v);
}
回答by mchr
Google for "Java Primitive Maps" and you will find some specialised types which avoid the need for autoboxing. An example of this is: https://labs.carrotsearch.com/hppc.html
谷歌搜索“Java Primitive Maps”,你会发现一些不需要自动装箱的特殊类型。一个例子是:https: //labs.carrotsearch.com/hppc.html
However, in general you should do fine with autoboxing as mentioned in other answers.
但是,通常您应该像其他答案中提到的那样使用自动装箱。
回答by jjnguy
You can do the following:
您可以执行以下操作:
Map<String, Integer> map = new HashMap<String, Integer>()
Then operations like:
然后操作如下:
map.put("One", 1);
will work. The primitive 1
will get auto-boxed into an Integer
. Likewise:
将工作。原语1
将被自动装箱到Integer
. 同样地:
int i = map.get("One");
will also work because the Integer
will get auto-unboxed into an int
.
也将起作用,因为Integer
将被自动拆箱到int
.
Check out some documentation on autoboxing and autounboxing.
查看有关autoboxing 和 autounboxing 的一些文档。
回答by Andreas Dolk
Every primitive has a wrapper class, like java.lang.Long
for long
.
每个原语都有一个包装类,比如java.lang.Long
for long
。
So you can map the the wrapper class to String
and, if you use Java 1.5+, simply put primitives to the map:
因此,您可以将包装类映射到String
,如果您使用 Java 1.5+,只需将原语放入映射中:
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("key", 10);
int value = map.get("key"); // value is 10.
回答by John Vint
You would use their boxed counterpart.
你会使用他们的盒装对应物。
Map<String,Integer> map = new HashMap<String,Integer>();
Integer is an immutable boxed type of the primitive int. There are similar Short, Long, Double, Float and Byte boxed types.
Integer 是原始 int 的不可变盒装类型。有类似的 Short、Long、Double、Float 和 Byte 盒装类型。
回答by Peter Lawrey
If you need the value to be a primitive for performance reasons, you can use TObjectIntHashMapor similar.
如果出于性能原因需要将该值作为原语,则可以使用TObjectIntHashMap或类似方法。
e.g.
例如
TObjectIntHashMap<String> map = new TObjectIntHashMap();
map.put("key", 10);
int value = map.get("key");
One difference with Map<String, Integer> is that the values are of type int primitive rather than Integer object.
与 Map<String, Integer> 的一个区别是值的类型是 int 原始类型而不是 Integer 对象。