可能重复:
Java:如何将列表转换为地图
我有arrayList
ArrayList<Product> productList = new ArrayList<Product>();
productList = getProducts(); //Fetch the result from db
我想像这样转换为ArrayList到HashMap
HashMap<String, Product> s= new HashMap<String,Product>();
请帮助我如何转换为HashMap。
一般的方法是遍历ArrayList
,然后将值插入HashMap
。示例如下:
HashMap<String, Product> productMap = new HashMap<String, Product>();
for (Product product : productList) {
productMap.put(product.getProductCode(), product);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句