package com.ideaheap;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class StreamsBasedCalculator {

    public Map<String, String> getStringTransition(
        final Set<String> stringSet,
        final Map<String, Double> stringCosts) {

        return stringCosts.keySet().parallelStream().collect(
            Collectors.toConcurrentMap(
                (state) -> state,
                (state) -> stringTransitionCost(state, stringSet, stringCosts)
            )
        );
    }

    private String stringTransitionCost(
        final String state,
        final Set<String> stringSet,
        final Map<String, Double> expectedUtilities) {

        return Double.toString(stringSet
        .stream()
        .mapToDouble(act -> calculateExpectedUtility(state, act, expectedUtilities))
        .max()
        .getAsDouble());
    }
//
    private Double calculateExpectedUtility(
        String state,
        String act,
        Map<String, Double> expectedUtilities) {

        return expectedUtilities
            .entrySet()
            .stream()
            .mapToDouble(
                n -> getStateProbability(state, act, n.getKey()) * n.getValue()
            ).sum();
    }

    private Double getStateProbability(String state, String act, String key) {
        return 5.0;
    }

}

@@ -704,33 +704,32 @@
 tedUtilities) %7B%0A
-%0A
         return D
@@ -731,42 +731,17 @@
 urn 
-Double.toString(stringSet%0A        
+stringSet
 .str
@@ -737,32 +737,37 @@
 ringSet.stream()
+.max(
 %0A        .mapToD
@@ -759,29 +759,58 @@
         
-.mapToDouble(
+    Comparator.comparing(%0A                
 act -%3E c
@@ -863,17 +863,16 @@
 ilities)
-)
 %0A       
@@ -872,21 +872,20 @@
         
-.max(
+    
 )%0A      
@@ -890,22 +890,14 @@
     
+)
 .get
-AsDouble()
+(
 );%0A 
@@ -905,10 +905,8 @@
   %7D%0A
-//
 %0A   
@@ -1359,9 +1359,8 @@
     %7D%0A%0A%7D
-%0A

Revert "first attempt to improve performance"