gpt4 book ai didi

Java 8 Lambda - 过滤两个 ArrayList

转载 作者:行者123 更新时间:2023-11-29 08:35:23 25 4
gpt4 key购买 nike

我有两个列表。第一个是区域 ID 列表,第二个是具有嵌套区域对象的节点列表。

我的nodeList的结构:

....{
"id": 6412,
"name": "303000201",
"description": "desc",
"organization": {
"id": 41065,
"name": "adad",
"address": null
},
"region": {
"id": 21,
"name": "Quba rayonu",
"code": "303"
},
"nodeType": {
"id": "WELL",
"name": "Quyu",
"description": null
},
"location": {
"id": 6412,
"latitude": 41.36735554,
"longitude": 48.5041245554
}} ......

和区域列表:

{
"regions": ["56", "44"]
}

我必须为区域 ID 过滤我的 nodeList。我用老方法来做,但我想用 lambda 表达式来做。我该怎么做?

我用谷歌搜索,尝试过,但它不起作用:/

result= nodeList.stream()
.filter(n -> regionIDList.equals(n.getRegion().getId().toString()))
.collect(Collectors.toList());

提前谢谢你。

最佳答案

假设 regionIDList 是一个 List,使用 contains而不是 equals:

result = nodeList.stream()
.filter(n -> regionIDList.contains(n.getRegion().getId().toString()))
.collect(Collectors.toList());

关于Java 8 Lambda - 过滤两个 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44472687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com