Java8: HashMap to HashMap using Stream / Map-Reduce / Collector

Map<String, String> x;
Map<String, Integer> y =
    x.entrySet().stream()
        .collect(Collectors.toMap(
            e -> e.getKey(),
            e -> Integer.parseInt(e.getValue())
        ));

It’s not quite as nice as the list code. You can’t construct new Map.Entrys in a map() call so the work is mixed into the collect() call.

Leave a Comment