Home > front end >  Remove item from Nested list Java Stream
Remove item from Nested list Java Stream

Time:05-15

I'll appreciate some help with this please. Assuming I have

            "bookingId": 4,
            "flights": [
                {   ...
                    "flightNumber": "AUS382",
                },
                {    ...
                    "flightNumber": "ZOD498",
                }
            ]
        },

and i want to remove flights with flightNumber of ZOD498 alone, how do i go about that?

I've tried passengerBooking.removeIf(e -> e.getFlights().stream().anyMatch(flightToRemove))

where a is a predicate - Predicate<Flight> flightToRemove = e -> "ZOD498".equals(e.getFlightNumber())

That kinda works, but removes the whole flight object, so the output now becomes

"bookingId": 4,
            "flights": [
            ]
        },

Please what modification do i need to make to remove a specific flight and have a result similar to

"bookingId": 4,
            "flights": [
                {   ...
                    "flightNumber": "AUS382",
                }
                
            ]
        },

Thank You

CodePudding user response:

Assuming you have a class PassengerBooking with a List of objects Flight, and that the passengerBooking variable represents a List of objects PassengerBooking.

You could traverse the items of your List. Then, for each PassengerBooking object retrieve the List of Flight with the corresponding getter and then invoke the removeIf method on the Collection returned.

public class Main {
    public static void main(String[] args) {
        List<PassengerBooking> passengerBooking = new ArrayList<>(List.of(
                new PassengerBooking(4, new ArrayList<>(List.of(
                        new Flight("AUS382"),
                        new Flight("ZOD498")
                )))
        ));

        System.out.println(passengerBooking);

        for (PassengerBooking passenger: passengerBooking){
            passenger.getFlights().removeIf(f -> f.getFlightNumber().equals("ZOD498"));
        }

        System.out.println(passengerBooking);
    }
}

Besides, if this is a common operation for your PassengerBooking class, you could think of implementing a custom method to remove a Flight by its id.

This is a simple assumption of your classes' implementation to test the code above.

class PassengerBooking {
    private int bookingId;
    private List<Flight> flights;

    public PassengerBooking(int bookingId, List<Flight> flights) {
        this.bookingId = bookingId;
        this.flights = flights;
    }

    public int getBookingId() {
        return bookingId;
    }

    public List<Flight> getFlights() {
        return flights;
    }

    //Custom utility method
    public void removeFlight(String flightID){
        flights.removeIf(f -> f.getFlightNumber().equals("ZOD498"));
    }

    @Override
    public String toString() {
        return String.format("%d => %s", bookingId, flights);
    }
}

class Flight {
    private String flightNumber;

    public Flight(String flightNumber) {
        this.flightNumber = flightNumber;
    }

    public String getFlightNumber() {
        return flightNumber;
    }

    @Override
    public String toString() {
        return flightNumber;
    }
}
  • Related