static Map<String, Map<String, Set<String>>> getRecommendedLayout(Map<String, Set<String>> hostGroupHosts,
                                                                    Map<String, Set<String>> hostGroupComponents,
                                                                    Function<String, String> componentToService) {
    Map<String, Set<String>> componentHostMap = hostGroupComponents.entrySet().stream()
      .flatMap(entry -> entry.getValue().stream().map(comp -> Pair.of(comp, entry.getKey()))) // component -> hostgroup
      .flatMap(cmpHg -> hostGroupHosts.get(cmpHg.getValue()).stream().map(host -> Pair.of(cmpHg.getKey(), host))) // component -> host
      .collect(groupingBy(Pair::getKey, mapping(Pair::getValue, toSet())));// group by component

    return componentHostMap.entrySet().stream().collect(
      groupingBy(
        cmpHost -> componentToService.apply(cmpHost.getKey()),
        toMap(Map.Entry::getKey, Map.Entry::getValue)));
  }
Reuse information from layout recommendation if it happened
ambari-server/src/main/java/org/apache/ambari/server/topology/addservice/StackAdvisorAdapter.java