This code will work if you want to access it as an ArrayList:
List<Integer> users = new ArrayList<Integer>();
Map<String, List<Integer>> hashMap = new HashMap<String, List<Integer>> ();
hashMap.put("users",users);
hashMap.get("users");
Your code won't work since you were storing it as an Object. So you would need a cast. And even then you use []
for arrays, not ArrayLists
If you want to use it as an array you can change the last line to:
hashMap.get("users").toArray()
And then you can use [1]
, or just do .get(1) on the initial code.
Alternatively, if you want to use hashMap as a property bag as someone validly mentioned you would need to do a cast:
((ArrayList<Integer>) hashMap.get("users"))
Then you can use users as an ArrayList
as you are getting an Object
by default from the HashMap
.