【发布时间】:2011-12-09 00:36:36
【问题描述】:
我有数组列表
ArrayList<Product> productList = new ArrayList<Product>();
productList = getProducts(); //Fetch the result from db
我想像这样将ArrayList转换成HashMap
HashMap<String, Product> s= new HashMap<String,Product>();
请帮助我如何转换为 HashMap。
【问题讨论】:
-
Product有唯一的属性吗? -
假设 field1 是 Product 类中的一个字段,所以你可以这样做
Map<String, Product> urMap = yourList.stream().collect(Collectors.toMap(Product::getField1, Function.identity()));
标签: java