I have a list of DijkstraWays objects:
List<DijkstraWays> dijkstraWaysList= ...
DijkstraWays has the following attributes and getters:
class DijkstraWays {
String name;
Double cost;
public String getName() {
return name;
}
public Double getCost() {
return cost;
}
}
I need to group this list by name and make the sum of cost. For this, I used the following code:
Map<String, Double> result = dijkstraWaysList.stream()
.collect(Collectors.groupingBy(DijkstraWays::getName,
Collectors.summingDouble(DijkstraWays::getCost)));
This code works, but it does not preserve the order of insertion of the elements in djkstraWaysList list. This is because Collectors.groupingBy returns a Map / HashMap, and this type does not preserve insertion orders.
How can I use Collectors.groupingBy and Collectors.summingDouble preserving the insertion order of the djkstraWaysList list? I found several examples on the internet where the result is transformed into a LinkedHashMap, which preserves this, but in none of them is it possible to insert the Collectors.summingDouble. Example:
dijkstraWays.stream()
.collect(Collectors.groupingBy(DijkstraWays::getName,
LinkedHashMap::new,
Collectors.toList()));
I also need the sum of cost, but I can't use it together with groupby in these examples with LinkedHashMap.
Thanks.