vendredi 12 juillet 2019

Convert nested foreach loop into stream

Im trying to convert 2 foreach loops and if statement into stream.

Here is what I want to convert:

for (ViewFlightRouteAirportType associatedAirportType : etopsAndNonEtopsAssociatedAirportTypes) {
                for (ViewFlightAirportDTO airport : flightRoute.getAirportsForType(associatedAirportType)) {
                    if ( airportIataCode.equals(airport.getIataCode()) ) {
                        addValueIfNotPresent(associatedFlights, associatedAirportType, flightData);
                    }
                }
            }

etopsAndNonEtopsAssociatedAirportTypes is an array. Here is what I wrote.

Arrays.stream(etopsAndNonEtopsAssociatedAirportTypes)
                    .forEach(associatedAirportType -> flightRoute.getAirportsForType(associatedAirportType)
                            .forEach(airport -> flightRoute.getAirportsForType(associatedAirportType)
                                    .stream()
                                    .filter(airportIataCode -> airportIataCode.equals(airport.getIataCode())) );

It is not eneded and doesnt work, but for me it looks ugly. How it should looks like?

Aucun commentaire:

Enregistrer un commentaire