Reputation: 1215
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) |
| | |
--------------------------------
Upvotes: 15
Views: 37256
Reputation: 4755
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(
Collectors.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.
Upvotes: 11
Reputation: 811
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)
)
);
Upvotes: 15