1

I have two lists. The first one is an ID list of regions, the second one is a node list which has nested region objects.

Structure of my 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
 }} ......

And region list:

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

I must filter my nodeList for region ID. I do it the old way but I want to do it with lambda expressions. How can I do it?

I googled, tried, but it doesn't work :/

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

Thank you in advance.

1 Answer 1

5

Assuming regionIDList is a List, use contains instead of equals:

result = nodeList.stream()
                 .filter(n -> regionIDList.contains(n.getRegion().getId().toString()))
                 .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you once again ;) It works . But still have a little problem . It return result correctly but there are many null objects inside result list after 1500th object :/ Is it make left join ?)) I don;t understand
I need to check if Node (n) is not null
nodeListFilteredForRegion = nodeList.stream().filter(n -> Objects.nonNull(n)) .filter(n -> regionIDList.contains(n.getRegion().getId().toString())).collect(Collectors.toList()); I have solved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.