Java Stream Operation flattenFeatureStreamToMap( Stream>>> stream)

Here you can find the source of flattenFeatureStreamToMap( Stream>>> stream)

Description

Convert a flatten nested stream(feature family, stream(feature name.

License

Apache License

Declaration

private static Map<String, Map<String, Double>> flattenFeatureStreamToMap(
        Stream<? extends Map.Entry<String, Stream<? extends Map.Entry<String, Double>>>> stream) 

Method Source Code

//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;
    }
}

Related

  1. findLast(Stream s)
  2. findLastOf(Stream stream)
  3. findStreamAmongst(Class clazz, Collection instances)
  4. firstValue(Stream stream)
  5. flatOptionals(Stream> list)
  6. gcd(IntStream numbers)
  7. getOSIllegalCharacterStream(String path)
  8. getStream(Iterable iterable)
  9. getStringStreamFromArray(String... ids)