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

前端 未结 5 1833
别那么骄傲
别那么骄傲 2021-02-04 17:11

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

相关标签:
5条回答
  • 2021-02-04 17:41

    How is the HashMap declaration expressed in that scope? 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. 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. Assuming your ArrayList contains your class MyClass, the line above might be:

    HashMap<String, ArrayList<MyClass>> dictMap
    
    0 讨论(0)
  • 2021-02-04 17:45
    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.

    0 讨论(0)
  • 2021-02-04 17:48

    The get method of the HashMap is returning an Object, but the variable current is expected to take a ArrayList:

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

    For the above code to work, the Object must be cast to an 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 Objects, and that should be changed as necessary.

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

    0 讨论(0)
  • 2021-02-04 17:53

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

    0 讨论(0)
  • 2021-02-04 17:54

    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.

    0 讨论(0)
提交回复
热议问题