Alex Rivera | Logout

How to add element into ArrayList in HashMap

Asked 2012-08-26T23:20:07.833
32

How to add element into ArrayList in HashMap?

    HashMap<String, ArrayList<Item>> Items = new HashMap<String, ArrayList<Item>>();
Edit
Report

1 Answer

9

First you have to add an ArrayList to the Map

ArrayList<Item> al = new ArrayList<Item>();

Items.add("theKey", al); 

then you can add an item to the ArrayLIst that is inside the Map like this:

Items.get("theKey").add(item);  // item is an object of type Item
answered 2012-08-26T23:44:05.837

Your Answer