Thing with streams, once you change your stream using an intermediate operation such as filter or map , you cannot return to the values ββfrom the previous operation. Therefore, you need to find workarounds to always (in your case, Module ) store the value that you want to use in your stream. In the following solution, I open two secondary streams using anyMatch operations, which give a positive value if the module contains an artifact containing the required key.
This should do the trick:
ModuleKey key = Optional.ofNullable(modules.stream().filter(m -> m.getArtifacts().stream() .anyMatch(a -> a.equals("ear"))).findFirst().orElse(modules.stream() .filter(m -> m.getArtifacts().stream().anyMatch(a -> a.equals("war"))) .reduce((a, b) -> b).orElse(null))).map(Module::getKey).orElse(null);
To find the first item, use findFirst , which returns Optional<T> . We associate this Optional with Optional#orElse , which expands it and, if empty, returns a different value (in this case, use reduce((a, b) -> b) to find the last element). If nothing is found, we do not want the code to NullPointerException . Therefore, before we call the getKey method, we will complete all this in Optional with Optional.ofNullable and set orElse to null to the default value specified in your code.
As a side note,
else if(!artifact.getType().equals("ear")) { if(artifact.getType().equals("war")) { } }
can be reduced to
else if(artifact.getType.equals("war")) { }
source share