Reducing Map by using Java 8 Stream API
26,151
Solution 1
It will work for you
Map<Integer, Double> collect = START.entrySet()
.stream()
.collect(
Collectors.toMap(
Map.Entry::getKey,
e -> e.getValue()
.values()
.stream()
.reduce(0d, (a, b) -> a + b)
)
);
Solution 2
This should be a good example:
public class Main {
public static void main(final String[] args) {
final Map<Integer, Map<String, Double>> tmp = new HashMap<>();
tmp.put(1, new HashMap<String, Double>() {{
put("1", 3.45);
put("2", 1.23);
put("3", 0.98);
}});
tmp.put(2, new HashMap<String, Double>() {{
put("1", 1.00);
put("2", 2.00);
put("3", 3.00);
}});
System.out.println(tmp.entrySet().stream()
.collect(
toMap(Map.Entry::getKey,
data ->
data.getValue()
.values().stream()
.mapToDouble(Number::doubleValue).sum())));
}
}
output will be {1=5.66, 2=6.0}
and all this does is takes entry set of map, gets a stream of it and collects to new map sums of inner map values.
Author by
Fab
Updated on July 09, 2022Comments
-
Fab almost 2 years
I have a Map in the following form:
Map<Integer, Map<String,Double>> START
Let INNER be the inner map, i.e.
Map<String,Double>
For example, I'd like to reduce START map in a new one
Map<Integer, Double> END
which have the same keys, but different values. In particular, for each key, I want the new Double value be the SUM of values in the INNER map for the corresponding key.
How could I achieve this by using JAVA 8's STREAM API?
Thanks everyone.
EDIT: A sample map is
------------------------------ | | 2016-10-02 3.45 | | ID1 | 2016-10-03 1.23 | | | 2016-10-04 0.98 | ------------------------------ | | 2016-10-02 1.00 | | ID2 | 2016-10-03 2.00 | | | 2016-10-04 3.00 | ------------------------------
e I'd like a new map like the following one:
-------------------------------- | | | | ID1 | SUM(3.45,1.23,0.98) | | | | -------------------------------- | | | | ID2 | SUM(1.00,2.00,3.00) | | | | --------------------------------
-
Fab over 7 yearsYour code gives me the following error: The method toMap(Map.Entry::getKey, (<no type> data) -> {}) is undefined for the type Start
-
balki over 7 yearsstackoverflow.com/a/39851906/463758 The other answer saves one lookup by streaming
entrySet()
instead ofkeySet()
. -
Prabhat Gaur over 2 yearswhat is meant by getValue().values()