Source:
<code>private PollingResult removeHostsFromOrchestrator(Stack stack, List<String> hostNames) throws CloudbreakException { LOGGER.debug("Remove hosts from orchestrator: [{}]", hostNames); try { Map<String, String> removeNodePrivateIPsByFQDN = new HashMap<>(); stack.getAllInstanceMetaDataList().stream() .filter(instanceMetaData -> Objects.nonNull(instanceMetaData.getDiscoveryFQDN())) .filter(instanceMetaData -> hostNames.stream() .anyMatch(hn -> hn.equals(instanceMetaData.getDiscoveryFQDN()))) <START> .forEach(instanceMetaData -> removeNodePrivateIPsByFQDN.put(instanceMetaData.getDiscoveryFQDN(), instanceMetaData.getPrivateIp())); <END> Set<InstanceMetaData> remainingInstanceMetaDatas = stack.getNotDeletedInstanceMetaDataList().stream() .filter(instanceMetaData -> !removeNodePrivateIPsByFQDN.containsValue(instanceMetaData.getPrivateIp())) .collect(Collectors.toSet()); Set<Node> remainingNodes = remainingInstanceMetaDatas.stream() .map(im -> new Node(im.getPrivateIp(), im.getPublicIp(), im.getInstanceId(), im.getInstanceGroup().getTemplate().getInstanceType(), im.getDiscoveryFQDN(), im.getInstanceGroup().getGroupName())) .collect(Collectors.toSet()); List<GatewayConfig> remainingGatewayConfigs = gatewayConfigService.getGatewayConfigs(stack, remainingInstanceMetaDatas); hostOrchestrator.tearDown(remainingGatewayConfigs, removeNodePrivateIPsByFQDN, remainingNodes, new StackBasedExitCriteriaModel(stack.getId())); } catch (CloudbreakOrchestratorException e) { LOGGER.info("Failed to delete orchestrator components while decommissioning: ", e); throw new CloudbreakException("Failed to delete orchestrator components while decommissioning: ", e); } return SUCCESS; }</code><technical_language>nit: instead of forEach put collect Collectors.toMap this stream extracted a method</technical_language>

Target:
private PollingResult removeHostsFromOrchestrator(Stack stack, List<String> hostNames) throws CloudbreakException { LOGGER.debug("Remove hosts from orchestrator: [{}]", hostNames); try { Map<String, String> removeNodePrivateIPsByFQDN = stack.getAllInstanceMetaDataList().stream() .filter(instanceMetaData -> Objects.nonNull(instanceMetaData.getDiscoveryFQDN())) .filter(instanceMetaData -> hostNames.stream() .anyMatch(hn -> hn.equals(instanceMetaData.getDiscoveryFQDN()))) .collect(Collectors.toMap(instanceMetaData -> instanceMetaData.getDiscoveryFQDN(), instanceMeataData -> instanceMeataData.getPrivateIp())); Set<InstanceMetaData> remainingInstanceMetaDatas = stack.getNotDeletedInstanceMetaDataList().stream() .filter(instanceMetaData -> !removeNodePrivateIPsByFQDN.containsValue(instanceMetaData.getPrivateIp())) .collect(Collectors.toSet()); Set<Node> remainingNodes = remainingInstanceMetaDatas.stream() .map(im -> new Node(im.getPrivateIp(), im.getPublicIp(), im.getInstanceId(), im.getInstanceGroup().getTemplate().getInstanceType(), im.getDiscoveryFQDN(), im.getInstanceGroup().getGroupName())) .collect(Collectors.toSet()); List<GatewayConfig> remainingGatewayConfigs = gatewayConfigService.getGatewayConfigs(stack, remainingInstanceMetaDatas); LOGGER.debug("Tearing down [{}]. The remaining nodes are [{}].", removeNodePrivateIPsByFQDN, remainingNodes); hostOrchestrator.tearDown(remainingGatewayConfigs, removeNodePrivateIPsByFQDN, remainingNodes, new StackBasedExitCriteriaModel(stack.getId())); } catch (CloudbreakOrchestratorException e) { LOGGER.info("Failed to delete orchestrator components while decommissioning: ", e); throw new CloudbreakException("Failed to delete orchestrator components while decommissioning: ", e); } return SUCCESS; }