我有一个流,我想明确地将它收集到 HashMap 中。目前,我正在做如下操作:
我想知道是否有一种使用Collectors显式获取HashMap的方法。
List<Item> listToTransform = //initialized and populated
HashMap<K, V> myHashMap = new HashMap<>();
listToTransform.stream().map(/* do my transformation here */)
.forEach(i -> myHashMap.put(i.getKey(), i.getValue()));
我想知道是否有一种使用Collectors显式获取HashMap的方法。
Collectors.toMap
和一个供应函数。 - khelwood