可能是重复问题:
Java:如何将List转换为Map
我有一个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。
Product
是否具有唯一的属性? - SimonCMap<String, Product> urMap = yourList.stream().collect(Collectors.toMap(Product::getField1, Function.identity()));
- Jad Chahine