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

前端 未结 5 1837
别那么骄傲
别那么骄傲 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: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.

提交回复
热议问题