简体   繁体   English

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

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

I'm storing data in a HashMap with (key: String, value: ArrayList).我正在使用(键:字符串,值:ArrayList)将数据存储在 HashMap 中。 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“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?我不明白这个...... HashMap 返回一个对象而不是我存储在其中的 ArrayList 作为值吗? How do I convert this object into an ArrayList?如何将此对象转换为 ArrayList?

Thank you.谢谢你。

How is the HashMap declaration expressed in that scope? HashMap 声明在该范围内如何表达? It should be:它应该是:

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. HashMap(或任何其他泛型实现)的实现正在处理对象。 The type information is there for type safety checks during compile time.类型信息用于编译期间的类型安全检查。 See the Generics documentation .请参阅泛型文档

Also note that ArrayList is also implemented as a generic class, and thus you might want to specify a type there as well.另请注意, ArrayList也作为泛型类实现,因此您可能还想在那里指定一个类型。 Assuming your ArrayList contains your class MyClass , the line above might be:假设您的ArrayList包含您的类MyClass ,上面的行可能是:

HashMap<String, ArrayList<MyClass>> dictMap
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.工作正常......也许你在我的代码中发现了你的错误。

I suppose your dictMap is of type HashMap , which makes it default to HashMap<Object, Object> .我想你的 dictMap 是HashMap类型,这使它默认为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>>如果您希望它更具体,请将其声明为HashMap<String, ArrayList> ,或者更好地声明为HashMap<String, ArrayList<T>>

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

The get method of the HashMap is returning an Object , but the variable current is expected to take a ArrayList : HashMapget方法返回一个Object ,但变量current预计采用ArrayList

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

For the above code to work, the Object must 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 ArrayList has a list of Object s, and that should be changed as necessary.上面的代码假设ArrayList有一个Object列表,并且应该根据需要进行更改。

For more information on generics, The Java Tutorials has a lesson on generics .有关泛型的更多信息,Java 教程有一堂关于泛型课程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数组列表 <HashMap<String, Object> ListView单元的按KEY值排序 - ArrayList<HashMap<String, Object> Order by KEY Value for ListView Cells 对Hashmap的ArrayList进行排序 <String,Object> 按整数值 - Sorting an ArrayList of Hashmap<String,Object> by int value String []或ArrayList更好地作为HashMap中的Key? - String[] or ArrayList better as Key in HashMap? 从ArrayList HashMap中的键/值中检索值<String, String> - Retrieving value from key/value in ArrayList HashMap<String, String> Java/Android - 搜索 ArrayList <hashmap<string, string> &gt; 用于匹配键 -&gt; 值</hashmap<string,> - Java/Android - Search ArrayList<HashMap<String, String>> for matching key -> value Android:获取整个数组,值/键在ArrayList中 <HashMap<String, String> &gt; - Android: Get the entire array the value/key is in ArrayList<HashMap<String, String>> 如何将对象数组列表转换为哈希表,其中键和值是对象属性 - How to convert a object arraylist into a hashmap where key and value are the objects attributes 哈希图,键为字符串,值为包含元素迭代问题的ArrayList - Hashmap with key as String and Value as ArrayList containing Elements Iteration issue 组ArrayList <HashMap<String, String> &gt;通过某个键 - Group ArrayList<HashMap<String, String>> by a certain key ArrayList作为HashMap的值仅返回ArrayList的最后一个元素 - ArrayList as a Value to HashMap returns only last element of ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM