Do not think in terms of loops, but rather what high-level operation you are doing, for example. finding the first item matching the criteria. The biggest obstacle to expressing such an operation is that Java methods cannot return two values. Using Map.Entry
as a type of pair containing both results you can use
Map.Entry<FootballTeam,Footballer> target = list.getFootballTeams()
.stream()
.flatMap(team -> team.getFootballers()
.stream()
.filter(f -> f.getId() == 1)
.map(f -> new AbstractMap.SimpleImmutableEntry<>(team, f)))
.findFirst()
.orElse(null);
, , , , .
list.getFootballTeams()
.stream()
.flatMap(team -> team.getFootballers()
.stream()
.filter(f -> f.getId() == 1)
.map(f -> new AbstractMap.SimpleImmutableEntry<>(team, f)))
.findFirst()
.ifPresent(e -> {
FootballTeam targetTeam = e.getKey();
Footballer targetFootballer = e.getValue();
});
, , , . .orElse(null)
, target
null
, , null
.