How to convert a list to HashMap

How can I convert this:

Map<String, Integer> itemsBought

So, I can add it to an ArrayList, as shown below:

public void add(String prd, int qty){
        orderList.add(new Order(prd, qty));
}

Are there other solutions:

hashMap.keySet().toArray(); 
hashMap.values().toArray(); 

Thanks in advance.

+3
source share
2 answers
for (Entry<String, Integer> entry : itemsBought.entrySet()) {
    orderList.add(new Order(entry.getKey(), entry.getValue()));
}
+11
source
for (String product : itemsBought.keySet()) {
    int quantity = itemsBought.get(product);
    orderList.add(new Order(product, quantity));
}

I prefer more readability (not so much)

+1
source

All Articles