Search code examples
java-streamcollectorsreducing

How to change the final type after reduction of a downstream collector in a Java 8 stream?


I got a legacy application using data structures like those in the following toy snippet and I can't easily change these data structures.

I use a Java 8 (only) stream to do some stats and I failed to get the wished type using Collectors.

package myIssueWithCollector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

public class MyIssueWithCollector {

    public static Double latitude(Map<String, String> map) {
    String latitude = map.get("LATITUDE");
    return Double.valueOf(latitude);
    }

    private static int latitudeComparator(double d1, double d2) {
    // get around the fact that NaN > +Infinity in Double.compare()
    if (Double.isNaN(d1) && !Double.isNaN(d2)) {
        return -1;
    }
    if (!Double.isNaN(d1) && Double.isNaN(d2)) {
        return 1;
    }
    return Double.compare(Math.abs(d1), Math.abs(d2));
    }

    public static Map<String, String> createMap(String city, String country, String continent, String latitude) {
    Map<String, String> map = new HashMap<>();
    map.put("CITY", city);
    map.put("COUNTRY", country);
    map.put("CONTINENT", continent);
    map.put("LATITUDE", latitude);
    return map;
    }

    public static void main(String[] args) {

    // Cities with dummies latitudes
    // I can not change easily these legacy data structures
    Map<String, String> map1 = createMap("London", "UK", "Europa", "48.1");
    Map<String, String> map2 = createMap("New York", "USA", "America", "42.4");
    Map<String, String> map3 = createMap("Miami", "USA", "America", "39.1");
    Map<String, String> map4 = createMap("Glasgow", "UK", "Europa", "49.2");
    Map<String, String> map5 = createMap("Camelot", "UK", "Europa", "NaN");

    List<Map<String, String>> maps = new ArrayList<>(4);
    maps.add(map1);
    maps.add(map2);
    maps.add(map3);
    maps.add(map4);
    maps.add(map5);

    //////////////////////////////////////////////////////////////////
    // My issue starts here:
    //////////////////////////////////////////////////////////////////
    Map<String, Map<String, Double>> result = maps.stream()
        .collect(Collectors.groupingBy(m -> m.get("CONTINENT"),
            Collectors.groupingBy(m -> m.get("COUNTRY"), Collectors.reducing(Double.NaN, m -> latitude(m),
                BinaryOperator.maxBy((d1, d2) -> latitudeComparator(d1, d2))))));

    System.out.println(result);
}
}

I need the result type to be Map<String, Map<String, String>> instead of Map<String, Map<String, Double>> by converting back "LATITUDE" from Double to String (using a custom format, not Double.toString() ).

I failed to achieve this with Collectors methods like andThen or collectingAndThen,...

I am currently stuck with Java 8.

Is there a way to get a Map<String, Map<String, String>> result using the same stream ?


Solution

  • You can use Collectors.collectingAndThen to convert the reduced double value to a corresponding String:

        Map<String, Map<String, String>> result = maps.stream().collect(
            Collectors.groupingBy(
                m -> m.get("CONTINENT"),
                Collectors.groupingBy(
                    m -> m.get("COUNTRY"),
                    Collectors.collectingAndThen(
                        Collectors.reducing(
                            Double.NaN,
                            m -> latitude(m),
                            BinaryOperator.maxBy(
                                (d1, d2) -> latitudeComparator(d1, d2)
                            )
                        ),
                        MyIssueWithCollector::myToString
                    )
                )
            )
        );
    

    Here, myToString is some method in the MyIssueWithCollector class to return String from double with your custom format, for example,

        public static String myToString(double d) {
            return "[latitude=" + d + "]";
        }