Java HashMap(key: String, value: ArrayList) 返回一个 Object 而不是 ArrayList?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/960807/
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-08-11 21:36:33  来源:igfitidea点击:

HashMap(key: String, value: ArrayList) returns an Object instead of ArrayList?

javaarraylist

提问by ck_

I'm storing data in a HashMap with (key: String, value: ArrayList). The part I'm having trouble with declares a new ArrayList "current," searches the HashMap for the String "dictCode," and if found sets current as the returned value ArrayList.

我正在使用(键:字符串,值:ArrayList)将数据存储在 HashMap 中。我遇到问题的部分声明了一个新的 ArrayList“current”,在 HashMap 中搜索字符串“dictCode”,如果找到,则将 current 设置为返回值 ArrayList。

ArrayList current = new ArrayList();      
if(dictMap.containsKey(dictCode)) {
    current = dictMap.get(dictCode);   
}

The "current =..." line returns a compiler error of:

“current =...”行返回编译器错误:

Error: incompatible types
found   : java.lang.Object
required: java.util.ArrayList

I don't understand this... does that HashMap return an Object instead of the ArrayList I stored in it as the value? How do I convert this object into an ArrayList?

我不明白这个...... HashMap 返回一个对象而不是我存储在其中的 ArrayList 作为值吗?如何将此对象转换为 ArrayList?

Thank you.

谢谢你。

采纳答案by Jared Oberhaus

How is the HashMap declaration expressed in that scope? It should be:

HashMap 声明在该范围内如何表达?它应该是:

HashMap<String, ArrayList> dictMap

If not, it is assumed to be Objects.

如果不是,则假定为对象。

For instance, if your code is:

例如,如果您的代码是:

HashMap dictMap = new HashMap<String, ArrayList>();
...
ArrayList current = dictMap.get(dictCode);

that will not work. Instead you want:

那不管用。相反,您想要:

HashMap<String, ArrayList> dictMap = new HashMap<String, Arraylist>();
...
ArrayList current = dictMap.get(dictCode);

The way generics work is that the type information is available to the compiler, but is not available at runtime. This is called type erasure. The implementation of HashMap (or any other generics implementation) is dealing with Object. The type information is there for type safety checks during compile time. See the Generics documentation.

泛型的工作方式是类型信息对编译器可用,但在运行时不可用。这称为类型擦除。HashMap(或任何其他泛型实现)的实现正在处理对象。类型信息用于编译期间的类型安全检查。请参阅泛型文档

Also note that ArrayListis also implemented as a generic class, and thus you might want to specify a type there as well. Assuming your ArrayListcontains your class MyClass, the line above might be:

另请注意,ArrayList它也是作为泛型类实现的,因此您可能还想在那里指定一个类型。假设您ArrayList包含您的 class MyClass,上面的行可能是:

HashMap<String, ArrayList<MyClass>> dictMap

回答by Jorn

I suppose your dictMap is of type HashMap, which makes it default to HashMap<Object, Object>. If you want it to be more specific, declare it as HashMap<String, ArrayList>, or even better, as HashMap<String, ArrayList<T>>

我想你的 dictMap 是类型的HashMap,这使它默认为HashMap<Object, Object>. 如果您希望它更具体,请将其声明为HashMap<String, ArrayList>,甚至更好,为HashMap<String, ArrayList<T>>

回答by C A

Using generics (as in the above answers) is your best bet here. I've just double checked and:

在这里使用泛型(如上面的答案)是最好的选择。我刚刚仔细检查了一下:

test.put("test", arraylistone); 
ArrayList current = new ArrayList();
current = (ArrayList) test.get("test");

will work as well, through I wouldn't recommend it as the generics ensure that only the correct data is added, rather than trying to do the handling at retrieval time.

也可以工作,因为我不会推荐它,因为泛型确保只添加正确的数据,而不是尝试在检索时进行处理。

回答by coobird

The getmethod of the HashMapis returning an Object, but the variable currentis expected to take a ArrayList:

get方法HashMap正在返回一个Object,但current预计该变量将采用一个ArrayList

ArrayList current = new ArrayList();
// ...
current = dictMap.get(dictCode);

For the above code to work, the Objectmust be cast to an ArrayList:

要使上述代码正常工作,Object必须将 强制转换为ArrayList

ArrayList current = new ArrayList();
// ...
current = (ArrayList)dictMap.get(dictCode);

However, probably the better way would be to use generic collection objects in the first place:

但是,可能更好的方法是首先使用泛型集合对象:

HashMap<String, ArrayList<Object>> dictMap =
    new HashMap<String, ArrayList<Object>>();

// Populate the HashMap.

ArrayList<Object> current = new ArrayList<Object>();      
if(dictMap.containsKey(dictCode)) {
    current = dictMap.get(dictCode);   
}

The above code is assuming that the ArrayListhas a list of Objects, and that should be changed as necessary.

上面的代码假设ArrayList有一个Objects列表,并且应该根据需要进行更改。

For more information on generics, The Java Tutorials has a lesson on generics.

有关泛型的更多信息,Java 教程有一堂关于泛型课程

回答by coobird

public static void main(String arg[])
{
    HashMap<String, ArrayList<String>> hashmap = 
        new HashMap<String, ArrayList<String>>();
    ArrayList<String> arraylist = new ArrayList<String>();
    arraylist.add("Hello");
    arraylist.add("World.");
    hashmap.put("my key", arraylist);
    arraylist = hashmap.get("not inserted");
    System.out.println(arraylist);
    arraylist = hashmap.get("my key");
    System.out.println(arraylist);
}

null
[Hello, World.]

Works fine... maybe you find your mistake in my code.

工作正常......也许你在我的代码中发现了你的错误。