正确的方式来初始化HashMap,并可以HashMap保存不同的值类型? [英] Correct way to initialize HashMap and can HashMap hold different value types?

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

问题描述

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


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

      HashMap x = new HashMap(); 

    但Eclipse始终暗示我使用:

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

    哪个更好?

  2. > HashMap 是否可以将不同类型的对象/数据类型保存为值?例如,这样做是否可行:

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

    在第一个 put()中,I需要一个 int 作为值,第二个是 int [] ,第三个字符串。这可以在Java中使用 HashMap s吗?另外,将 HashMap 作为一个值存储在 HashMap

    中可以吗?


解决方案

确实取决于您需要的安全类型。这种非泛型方法最好是这样做的:

  Map x = new HashMap(); 

请注意, x 键入为地图。这使得未来更改实现变得容易(对 TreeMap LinkedHashMap )。



您可以使用泛型来确保某种类型的安全级别:

  Map<字符串,对象> x = new HashMap< String,Object>(); 

在Java 7及更高版本中,您可以执行

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

上面虽然比较详细,但避免了编译器警告。在这种情况下, HashMap 的内容可以是任何 Object ,因此可以是 Integer , int [] 等等,这就是你正在做的事情。



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



顺便说一下,当您添加int或其他Java是自动装箱的。这意味着代码相当于:

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

您当然可以将 HashMap 作为值在另一个 HashMap ,但我认为有问题,如果你递归做(这是把 HashMap 作为一个价值本身)。


So I have two questions about HashMaps in Java:

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

    But Eclipse keeps suggesting that I use:

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

    Which is better?

  2. 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");
    

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

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

In Java 7 and later you can do

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

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.

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

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

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保存不同的值类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆