How to apply multiple Filters on Java Stream?

后端 未结 4 757
梦毁少年i
梦毁少年i 2021-01-11 09:27

I have to filter a Collection of Objects by a Map, which holds key value pairs of the Objects field names and field values. I am trying to apply all filters by stream().filt

4条回答
  •  感情败类
    2021-01-11 10:18

    To apply a variable number of filter steps to a stream (that only become known at runtime), you could use a loop to add filter steps.

    Stream stream = list.stream();
    for (Predicate predicate: allPredicates) {
      stream = stream.filter(predicate);
    }
    list = stream.collect(Collectors.toList());
    

提交回复
热议问题