// "Collapse loop with stream 'collect()'" "true-preview" import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { List test(Map> map, int limit) { List list = map.entrySet().stream().filter(entry -> entry.getValue() != null).filter(entry -> entry.getValue().stream().anyMatch(str -> str.contains("foo"))).limit(limit).map(Map.Entry::getKey).collect(Collectors.toList()); return list; } }