Java 映射和原语

发布于 2024-10-06 12:24:22 字数 229 浏览 0 评论 0原文

我想创建一个以 String 作为键、以 primitive 作为值的映射。我正在查看 Java 文档,但没有看到 Primitive 是一种类类型,或者它们共享某种包装类。

如何将值限制为原始值?

Map<字符串,基元> map = new HashMap();

I want to create a mapping that takes a String as the key and a primitive as 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.

How can I constrain the value to be a primitive?

Map<String, Primitive> map = new HashMap<String, Primitive>();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

胡渣熟男 2024-10-13 12:24:22

Java Autoboxing 允许在 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

如果您想在一个映射中存储不同的基本类型,您可以通过创建一个Map来实现。允许存储 BigDecimalBigIntegerByteDoubleFloatIntegerLongShort(以及AtomicLongAtomicInteger)。

这是一个例子:

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);
}

Java Autoboxing allows to create maps on Long, Integer, Double and then operate them using primitive values. For example:

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).

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);
}
梦里寻她 2024-10-13 12:24:22

谷歌搜索“Java Primitive Maps”,您会发现一些专门的类型,可以避免自动装箱的需要。一个例子是: https://labs.carrotsearch.com/hppc.html

但是,一般来说,您应该像其他答案中提到的那样使用自动装箱。

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

However, in general you should do fine with autoboxing as mentioned in other answers.

说好的呢 2024-10-13 12:24:22

您可以执行以下操作:

Map<String, Integer> map = new HashMap<String, Integer>()

然后像这样的操作

map.put("One", 1);

将起作用。基元 1 将自动装箱为 Integer。同样:

int i = map.get("One");

也会起作用,因为Integer将自动拆箱为int

查看有关自动装箱和自动拆箱的一些文档。

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:

int i = map.get("One");

will also work because the Integer will get auto-unboxed into an int.

Check out some documentation on autoboxing and autounboxing.

挽容 2024-10-13 12:24:22

每个原语都有一个包装类,例如 java.lang. lang.Long 表示 long

因此,您可以将包装类映射到 String,如果您使用 Java 1.5+,只需将原语放入映射即可:

 Map<String, Integer> map = new HashMap<String, Integer>();
 map.put("key", 10);
 int value = map.get("key");  // value is 10.

Every primitive has a wrapper class, like java.lang.Long for long.

So you can map the the wrapper class to Stringand, if you use Java 1.5+, simply put primitives to the map:

 Map<String, Integer> map = new HashMap<String, Integer>();
 map.put("key", 10);
 int value = map.get("key");  // value is 10.
彩扇题诗 2024-10-13 12:24:22

你会使用他们的盒装对应物。

Map<String,Integer> map = new HashMap<String,Integer>();

Integer 是原始 int 的不可变装箱类型。还有类似的 Short、Long、Double、Float 和 Byte 装箱类型。

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.

情场扛把子 2024-10-13 12:24:22

如果出于性能原因需要将该值设为基元,可以使用 TObjectIntHashMap 或类似的。

例如

TObjectIntHashMap<String> map = new TObjectIntHashMap();

map.put("key", 10);
int value = map.get("key");

与 Map的一个区别是值是 int 基元类型而不是 Integer 对象类型。

If you need the value to be a primitive for performance reasons, you can use TObjectIntHashMap or similar.

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.

醉酒的小男人 2024-10-13 12:24:22

您不能将基元作为 Map 接口中的键或值。相反,您可以使用包装类,例如 IntegerCharacterBoolean 等。

阅读wiki了解更多信息。

You can't have a primitive as key or value in Map interface. Instead you can use Wrapper classes, like Integer, Character, Boolean and so on.

Read more on wiki.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文