从嵌套列表 Java 流中删除项目
Remove item from Nested list Java Stream
请提供一些帮助,我将不胜感激。假设我有
"bookingId": 4,
"flights": [
{ ...
"flightNumber": "AUS382",
},
{ ...
"flightNumber": "ZOD498",
}
]
},
我想单独删除 flightNumber
个 ZOD498
的航班,我该怎么做?
我试过了
passengerBooking.removeIf(e -> e.getFlights().stream().anyMatch(flightToRemove))
其中 a
是谓词 - Predicate<Flight> flightToRemove = e -> "ZOD498".equals(e.getFlightNumber())
这有点管用,但是删除了整个 flight
对象,所以输出现在变成了
"bookingId": 4,
"flights": [
]
},
请问我需要做哪些修改才能删除特定航班并得到类似于
的结果
"bookingId": 4,
"flights": [
{ ...
"flightNumber": "AUS382",
}
]
},
谢谢
假设您有一个 class PassengerBooking
和 List
个对象 Flight
,并且 passengerBooking
变量表示 List
对象 PassengerBooking
.
您可以遍历 List
的项目。然后,对于每个 PassengerBooking
对象,使用相应的 getter 检索 Flight 的 List
,然后在返回的 Collection
上调用 removeIf
方法。
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);
}
}
此外,如果这是您的 PassengerBooking
class 的常见操作,您可以考虑实现一个自定义方法来通过其 id 删除 Flight
。
这是对您的 classes 实施的简单假设,用于测试上面的代码。
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;
}
}
请提供一些帮助,我将不胜感激。假设我有
"bookingId": 4,
"flights": [
{ ...
"flightNumber": "AUS382",
},
{ ...
"flightNumber": "ZOD498",
}
]
},
我想单独删除 flightNumber
个 ZOD498
的航班,我该怎么做?
我试过了
passengerBooking.removeIf(e -> e.getFlights().stream().anyMatch(flightToRemove))
其中 a
是谓词 - Predicate<Flight> flightToRemove = e -> "ZOD498".equals(e.getFlightNumber())
这有点管用,但是删除了整个 flight
对象,所以输出现在变成了
"bookingId": 4,
"flights": [
]
},
请问我需要做哪些修改才能删除特定航班并得到类似于
的结果"bookingId": 4,
"flights": [
{ ...
"flightNumber": "AUS382",
}
]
},
谢谢
假设您有一个 class PassengerBooking
和 List
个对象 Flight
,并且 passengerBooking
变量表示 List
对象 PassengerBooking
.
您可以遍历 List
的项目。然后,对于每个 PassengerBooking
对象,使用相应的 getter 检索 Flight 的 List
,然后在返回的 Collection
上调用 removeIf
方法。
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);
}
}
此外,如果这是您的 PassengerBooking
class 的常见操作,您可以考虑实现一个自定义方法来通过其 id 删除 Flight
。
这是对您的 classes 实施的简单假设,用于测试上面的代码。
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;
}
}