使用 Java 中的流过滤内部列表
Filter inner list using stream in Java
我想为我的产品类别列表应用第二个过滤器,如下所示:
final List<ProductCategoryDTO> productCategoryList = productCategoryService
.findAllByUuid(uuid)
.stream()
.filter(category -> !category.getProductList().isEmpty())
// I am trying to filter the productCategoryList based on
// the "isDisabled" value of inner list >>>
.filter(category -> category.getProductList().stream()
.anyMatch(p -> !p.getMenuProperties().isDisabled()))
.collect(Collectors.toList());
第一个过滤器 !category.getProductList().isEmpty()
应用于 productCategoryList(外部列表),然后我还想根据内部列表的 isDisabled
值应用过滤器。我尝试使用 flatMap
并连接过滤条件,如下所示,但其中 none 不起作用:
.filter(category -> !category.getProductList().isEmpty() &&
!category.getProductList().stream()
.anyMatch(p -> p.getMenuProperties().isDisabled()))
那么,如何根据内部列表值过滤 productCategoryList
?
以下是相关的 DTO:
public class ProductCategoryDTO {
private UUID uuid;
private List<MenuCategoryDTO> productList;
}
public class MenuCategoryDTO {
private MenuPropertiesDTO menuProperties;
}
更新: 我还需要通过展平列表来检索产品的 UUID 值列表,如下所示。但它不起作用:
final List<UUID> productUuidList = productCategoryList.stream()
.flatMap(a -> a.getProductList().stream())
.flatMap(b -> b.getMenuProperties().getUuid())
.collect(Collectors.toList());
有什么想法吗?
如果您的 category.getProductList()
不是不可变列表,那么您可以使用
List<ProductCategoryDTO> list = productCategoryService.findAllByUuid(uuid);
list.stream()
.filter(category -> !category.getProductList().isEmpty())
.forEach(category -> category.getProductList()
.removeIf(p -> p.getMenuProperties().isDisabled()));
您在第一个 flatMap 之后获得了 MenuCategoryDTO 对象的 Stream。所以你不需要第二个平面地图。您可以改用地图。
final List<UUID> productUuidList = productCategoryList.stream()
.flatMap(a -> a.getProductList().stream())
.map(b -> b.getMenuProperties().getUuid())
.collect(Collectors.toList());
我想为我的产品类别列表应用第二个过滤器,如下所示:
final List<ProductCategoryDTO> productCategoryList = productCategoryService
.findAllByUuid(uuid)
.stream()
.filter(category -> !category.getProductList().isEmpty())
// I am trying to filter the productCategoryList based on
// the "isDisabled" value of inner list >>>
.filter(category -> category.getProductList().stream()
.anyMatch(p -> !p.getMenuProperties().isDisabled()))
.collect(Collectors.toList());
第一个过滤器 !category.getProductList().isEmpty()
应用于 productCategoryList(外部列表),然后我还想根据内部列表的 isDisabled
值应用过滤器。我尝试使用 flatMap
并连接过滤条件,如下所示,但其中 none 不起作用:
.filter(category -> !category.getProductList().isEmpty() &&
!category.getProductList().stream()
.anyMatch(p -> p.getMenuProperties().isDisabled()))
那么,如何根据内部列表值过滤 productCategoryList
?
以下是相关的 DTO:
public class ProductCategoryDTO {
private UUID uuid;
private List<MenuCategoryDTO> productList;
}
public class MenuCategoryDTO {
private MenuPropertiesDTO menuProperties;
}
更新: 我还需要通过展平列表来检索产品的 UUID 值列表,如下所示。但它不起作用:
final List<UUID> productUuidList = productCategoryList.stream()
.flatMap(a -> a.getProductList().stream())
.flatMap(b -> b.getMenuProperties().getUuid())
.collect(Collectors.toList());
有什么想法吗?
如果您的 category.getProductList()
不是不可变列表,那么您可以使用
List<ProductCategoryDTO> list = productCategoryService.findAllByUuid(uuid);
list.stream()
.filter(category -> !category.getProductList().isEmpty())
.forEach(category -> category.getProductList()
.removeIf(p -> p.getMenuProperties().isDisabled()));
您在第一个 flatMap 之后获得了 MenuCategoryDTO 对象的 Stream。所以你不需要第二个平面地图。您可以改用地图。
final List<UUID> productUuidList = productCategoryList.stream()
.flatMap(a -> a.getProductList().stream())
.map(b -> b.getMenuProperties().getUuid())
.collect(Collectors.toList());