Possible Duplicate:
Java: How to convert List to Map
I have arrayList
ArrayList<Product> productList = new ArrayList<Product>();
productList = getProducts(); //Fetch the result from db
I want to convert to ArrayList to HashMap Like this
HashMap<String, Product> s= new HashMap<String,Product>();
Please help me how to convert to HashMap.
The general methodology would be to iterate through the ArrayList
, and insert the values into the HashMap
. An example is as follows:
HashMap<String, Product> productMap = new HashMap<String, Product>();
for (Product product : productList) {
productMap.put(product.getProductCode(), product);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With