2017-06-10 37 views
1

我有兩個列表。第一個是區域的ID列表,第二個是具有嵌套區域對象的節點列表。Java 8 Lambda - 過濾器2 ArrayList

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"] 
} 

我必須篩選我nodeList的區域ID。我以舊的方式做,但我想用lambda表達式來做。我該怎麼做?

我GOOGLE了,盡力了,但它不工作:/

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

預先感謝您。

回答

3

假設regionIDListList,使用contains代替equals

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

喔謝謝您的回答,這可能是原因。我會盡力回到你身邊。 –

+0

再次感謝你;)它的工作原理。但還是有一點問題。它正確地返回結果,但是在第1500個對象之後,結果列表中有很多空對象:/它是否使左連接?))我不明白 –

+0

我需要檢查Node(n)是否爲空 –