示例 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"
}
]
}
]
我想检索至少具有一种活动产品的对象列表。
下面的代码返回产品列表,但我想要一个列表ProdcutResponse
。有办法做到这一点吗?
response.stream()
.map(ProductResponse::getProducts)
.filter(s -> "Active".equals(s.getType()))
.collect(Collectors.toList())
因为您没有展示太多代码,所以这将是一个盲目的尝试。我的主要建议是不要映射Stream<ProductResponse>
to a Stream<List<Product>>
在将其收集到列表之前:
response.stream()
.filter(pr -> pr.getProducts().stream().anyMatch(s -> "Active".equals(s.getType())))
.collect(Collectors.toList());
你可以改变anyMatch
to allMatch
如果你想要List<ProductResponse>
包含装有onlyProduct
具有活动类型(无论这意味着什么)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)