样本JSON
[
{
"id": "1",
"products": [
{
"id": "333",
"status": "Active"
},
{
"id": "222",
"status": "Inactive"
},
{
"id": "111",
"status": "Active"
}
]
},
{
"id": "2",
"products": [
{
"id": "6",
"status": "Active"
},
{
"id": "7",
"status": "Inactive"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
我想检索具有至少一个活动产品的对象列表。
下面的代码返回产品列表,但我想要一个列表ProdcutResponse。有什么办法吗?
response.stream()
.map(ProductResponse::getProducts)
.filter(s -> "Active".equals(s.getType()))
.collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)
因为您没有显示太多代码,所以这将是黑暗中的一枪。我的主要建议是在Stream<ProductResponse>将其Stream<List<Product>>收集到列表之前不要将其映射到:
response.stream()
.filter(pr -> pr.getProducts().stream().anyMatch(s -> "Active".equals(s.getType())))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果要仅包含具有活动类型的s(无论如何),可以更改anyMatch为。allMatchList<ProductResponse>Product
| 归档时间: |
|
| 查看次数: |
4723 次 |
| 最近记录: |