Use Java filter on stream with in a stream filter

Harshana :

I have Item and Address classes:

public class Item {
    String name;
    List<Address> address;
}

public class Address {  
    String name;
    String lane;
}

Suppose I have a item list and want to filter items that has lane as "lane1".

I try below but in Eclipse it shows:

"Type mismatch: cannot convert from Stream Address to boolean"

items.stream().filter(a->a.getAddress().stream().
      filter(b->b.getLane().equals("lane1"))).collect(Collectors.toList());
Sweeper :

You can use anyMatch on the inner stream:

items.stream().filter(a->a.getAddress().stream().
      anyMatch(b->"lane1".equals(b.getLane()))).collect(Collectors.toList());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=140037&siteId=1