Код: Выделить всё
Map data = new HashMap();
for (Scooter scooter : scooters) {
scooter.getRentalDays().iterator().forEachRemaining(e -> {
int newVal = scooter.getMileage() * scooter.getMileageRate();
int curVal = data.getOrDefault(e.getMonth(), 0);
data.put(e.getMonth(), curVal + newVal);
});
}
Map data = projects.stream()
.flatMap(scooter -> scooter.getRentalDays().stream()
.map(date -> Map.entry(date.getMonth(), scooter.getMileage() * scooter.getMileageRate())))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue, // this value needs to be a sum of the previous one.
(k1, k2) -> k1));
< /code>
Любые советы о том, как решить эту проблему? Идея состоит в том, чтобы иметь полное решение с потоками.
Подробнее здесь: https://stackoverflow.com/questions/650 ... -to-stream