有沒有什麼辦法可以在lambda函數內創建這個hashmap?如何創建一個HashMap來迭代lambda函數?
Map<SaleStatus, Long> sales = new HashMap<>();
saleStatusCounters.forEach(saleStatusCounter -> sales.put(saleStatusCounter.getStatus(), saleStatusCounter.getMatches()));
事情是這樣的:
saleStatusCounters.stream()
.map(obj -> new HashMap<SaleStatus, Long>().put(obj.getStatus(), obj.getMatches()))
.collect(Collectors.toMap(???)));
閱讀[文檔](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#toMap-java.util.function.Function-java.util .function.Function - )... –
你究竟想在這裏做什麼?爲什麼'Collectors.toMap'不夠? – chrylis