初始化HashMap的正确方法,HashMap可以保存不同的值

Correct way to initialize HashMap and can HashMap hold different value types?(初始化HashMap的正确方法,HashMap可以保存不同的值类型吗?)

本文介绍了初始化HashMap的正确方法,HashMap可以保存不同的值类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个关于 Java 中的 HashMaps 的问题:

So I have two questions about HashMaps in Java:

  1. 初始化HashMap的正确方法是什么?我认为在我的情况下使用它可能是最好的:

  1. What is the correct way to initialize a HashMap? I think it might be best in my situation to use:

HashMap x = new HashMap();

但 Eclipse 一直建议我使用:

But Eclipse keeps suggesting that I use:

HashMap<something, something> map = new HashMap();

哪个更好?

HashMap 可以保存不同类型的对象/数据类型作为值吗?例如,这是否可行并且可以:

Can a HashMap hold different types of objects/data types as values? For example, would this work and be OK:

map.put("one", 1);
map.put("two", {1, 2});
map.put("three", "hello");

在第一个 put() 中,我想要一个 int 作为值,第二个是 int[],第三个是细绳.在 Java 中使用 HashMap 可以这样做吗?另外,是否可以将 HashMap 作为值存储在 HashMap 中?

In the first put(), I want an int as a value, in the second an int[], and third a string. Is this okay to do in Java with HashMaps? Also, is it okay to store a HashMap as a value within a HashMap?

推荐答案

这真的取决于你需要什么样的类型安全.非泛型的做法最好如下:

It really depends on what kind of type safety you need. The non-generic way of doing it is best done as:

 Map x = new HashMap();

请注意,x 被键入为 Map.这使得将来更改实现(到 TreeMapLinkedHashMap)变得更加容易.

Note that x is typed as a Map. this makes it much easier to change implementations (to a TreeMap or a LinkedHashMap) in the future.

您可以使用泛型来确保一定程度的类型安全:

You can use generics to ensure a certain level of type safety:

Map<String, Object> x = new HashMap<String, Object>();

在 Java 7 及更高版本中,您可以这样做

In Java 7 and later you can do

Map<String, Object> x = new HashMap<>();

以上内容虽然更详细,但避免了编译器警告.在这种情况下,HashMap的内容可以是任何Object,所以可以是Integerint[]等,这就是你正在做的事情.

The above, while more verbose, avoids compiler warnings. In this case the content of the HashMap can be any Object, so that can be Integer, int[], etc. which is what you are doing.

如果您仍在使用 Java 6,Guava 库(虽然这很容易足够自己做)有一个名为 newHashMap() 避免了在执行 new 时需要复制通用类型信息.它从变量声明中推断出类型(这是 Java 7 之前的构造函数中不可用的 Java 特性).

If you are still using Java 6, Guava Libraries (although it is easy enough to do yourself) has a method called newHashMap() which avoids the need to duplicate the generic typing information when you do a new. It infers the type from the variable declaration (this is a Java feature not available on constructors prior to Java 7).

顺便说一句,当您添加一个 int 或其他原语时,Java 会自动装箱它.这意味着代码相当于:

By the way, when you add an int or other primitive, Java is autoboxing it. That means that the code is equivalent to:

 x.put("one", Integer.valueOf(1));

您当然可以将 HashMap 作为值放在另一个 HashMap 中,但我认为如果您以递归方式执行此操作(即放入 HashMap 本身就是一个值).

You can certainly put a HashMap as a value in another HashMap, but I think there are issues if you do it recursively (that is put the HashMap as a value in itself).

这篇关于初始化HashMap的正确方法,HashMap可以保存不同的值类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:初始化HashMap的正确方法,HashMap可以保存不同的值

基础教程推荐