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

0

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

Zahra

2 Answers

New Answer

+1

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

+1

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");