If you want to use functional style, you can create a stream from you map entry set, then expand it to get a stream of each item in underlying lists :
Optional<String> result = groupOfItems.entrySet().stream()
.flatMap(entry -> entry.getValue().stream())
.filter(item -> Types.TYPE_12.equals(item.getType))
.map(Item::getId)
.findAny();
result.ifPresent(id -> System.out.println("A match has been extracted: "+id));
As is, the functional style way is not more performant than the imperative one, but is more easily adaptable. Let's say you want to know if there's more than one match, you can replace findAny
by a collector with a limit :
List<String> matchedIds = groupOfItems.entrySet().stream()
.flatMap(entry -> entry.getValue().stream())
.filter(item -> Types.TYPE_12.equals(item.getType))
.map(Item::getId)
.limit(2)
.collect(Collectors.toList());
if (matched.isEmpty()) System.out.println("No match found");
else if (matched.size() == 1) System.out.println("Exactly one match found: "+matched.get(0));
else System.out.println("At least two matches exist");
Stream usage also allow parallelization if necessary, by simply adding parallel()
step to your pipeline.