简体   繁体   中英

How to convert List to a HashMap

How can I convert this:

Map<String, Integer> itemsBought

So that I can add it on the ArrayList as Follows:

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

Are there other solutions beside this:

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

Thank you in advance.

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

I prefer more readability (not much though)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM