How to return from forEach lambda when filtering by a set of iterable element

I have one List of items. Inside the list item in my case, say BeanI have another list. My requirement is that, iterating over the parent list, I have to check the specific condition in the list obtained from Bean class getList()and return a boolean value there. Below is a demo of the code I want to achieve. How to do this in JAVA -8 using lambda.?

public boolean test(List<Bean> parentList) {

    //Bean is having another List of Bean1
    // i want to do some thing like below 
     parentList.forEach(bean ->   
      bean.getList().stream().
               filter(somePredicate).
               findFirst().isPresent();
 }
+4
source share
1 answer

You should use Stream::flatMapand check your condition:

parentList.stream().flatMap(bean -> bean.getList().stream()).anyMatch(somePredicate);
+7
source

Source: https://habr.com/ru/post/1679401/


All Articles