Filter Sequence - Does It Matter?

Let's review what we have the following boolean condition
A,B,C,D.
Now we want to filter the list according to them:
list.stream().filter(x -> A).filter(x -> B).filter(x -> C).filter(x -> D)

list.stream().filter(x -> A && B && C && D)

Which way is better? I mean efficiency and elegance.

+4
source share
2 answers

In terms of feed efficiency, the combined filter will be a little better. But cases where it really matters to your application should be very rare. I would consider it as premature optimization .

And the cost of introducing such optimizations is closely related, prone to code errors, which does for many things at once and cannot be easily tested.

, . , .

+4

, ; filter, filter . .

? .

, , .

+1

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


All Articles