Here you can find the source of flattenFeatureStreamToMap( Stream extends Map.Entry
private static Map<String, Map<String, Double>> flattenFeatureStreamToMap( Stream<? extends Map.Entry<String, Stream<? extends Map.Entry<String, Double>>>> stream)
//package com.java2s; //License from project: Apache License import java.util.HashMap; import java.util.Map; import java.util.stream.Stream; public class Main { /**/*from www. ja v a2s.c o m*/ * Convert a flatten nested stream(feature family, stream(feature name. feature value)) to nested * map of feature family -> (feature -> value) */ private static Map<String, Map<String, Double>> flattenFeatureStreamToMap( Stream<? extends Map.Entry<String, Stream<? extends Map.Entry<String, Double>>>> stream) { Map<String, Map<String, Double>> outputFeatureMap = new HashMap<>(); stream.forEach(inputFamilyEntry -> { String familyName = inputFamilyEntry.getKey(); Map<String, Double> outputFeatureFamily = outputFeatureMap.get(familyName); if (outputFeatureFamily == null) { outputFeatureFamily = new HashMap<>(); outputFeatureMap.put(familyName, outputFeatureFamily); } // NB: this is necessary due to stream semantic where variable inside forEach has to be final final Map<String, Double> finalFeatures = outputFeatureFamily; inputFamilyEntry.getValue().forEach(feature -> finalFeatures.put(feature.getKey(), feature.getValue())); }); return outputFeatureMap; } }