Hashmap and arraylist | Sololearn: Learn to code for FREE!


Hashmap and arraylist

How do I add an arraylist to a hashmap in java when the key is name of the objects in arraylist?

7/25/2020 11:02:55 AM


2 Answers

New Answer


if class Obj has String name; field then for (Obj o : array) map.put(o.name, o); //or array.forEach(o -> map.put(o.name, o) );


May be like this, If not then please provide more details about your question HashMap<String, String> myHashmap = new HashMap<String, String>(); pseudo code for each element K in your ArrayList myHashMap.put(K, "some_value");