java – How to map over multiple items with Java 8 streams?

It’s an interesting question, because it shows that there are many different approaches to achieving the same result. Below I show three different implementations.

Methods predefined in the Collection Framework: Java 8 has added some methods to the collection classes, which are not directly related to the Stream API . Using these methods, you can significantly simplify the implementation of the non-stream implementation:

Collection<DataSet> convert(List<MultiDataPoint> multiDataPoints) {
    Map<String, DataSet> result = new HashMap<>();
    multiDataPoints.forEach(pt ->
        pt.keyToData.forEach((key, value) ->
                key, k -> new DataSet(k, new ArrayList<>()))
            .dataPoints.add(new DataPoint(pt.timestamp, value))));
    return result.values();

Stream API with flattening and intermediate data structure: The following implementation is almost identical to the solution provided by Stuart Marks. Contrary to its workaround, the following implementation uses a anonymous internal class as an intermediate data structure.

Collection<DataSet> convert(List<MultiDataPoint> multiDataPoints) {
        .flatMap(mdp -> mdp.keyToData.entrySet().stream().map(e ->
            new Object() {
                String key = e.getKey();
                DataPoint dataPoint = new DataPoint(mdp.timestamp, e.getValue());
                groupingBy(t -> t.key, mapping(t -> t.dataPoint, toList())),
                m -> m.entrySet().stream().map(e -> new DataSet(e.getKey(), e.getValue())).collect(toList())));

Stream API with Map Blend: Instead of flattening the original data structures, you can also create one Map for each MultiDataPoint , then merge all the maps into a single map with a reduce operation. The code is a little simpler than the solution above:

Collection<DataSet> convert(List<MultiDataPoint> multiDataPoints) {
        .map(mdp -> mdp.keyToData.entrySet().stream()
            .collect(toMap(e -> e.getKey(), e -> asList(new DataPoint(mdp.timestamp, e.getValue())))))
        .reduce(new HashMap<>(), mapMerger())
        .map(e -> new DataSet(e.getKey(), e.getValue()))

You can find an implementation of map merging within the class Collectors . Unfortunately, it is a bit tricky to access it from the outside. Below is an alternative implementation of map blending :

<K, V> BinaryOperator<Map<K, List<V>>> mapMerger() {
    return (lhs, rhs) -> {
        Map<K, List<V>> result = new HashMap<>();
        lhs.forEach((key, value) -> result.computeIfAbsent(key, k -> new ArrayList<>()).addAll(value));
        rhs.forEach((key, value) -> result.computeIfAbsent(key, k -> new ArrayList<>()).addAll(value));
        return result;

Leave a comment