Is Java stream filter smart enough to ignore unnecessary items in an ordered stream?
06:44 30 Aug 2024

Let's say I have the following:

List orderedList = Stream.of(5, 4, 0, 2, 1).sorted().toList();

If I apply a filter such as

List filteredList = orderedList.stream().filter(integer -> integer < 3).toList();

Will filter check all items in orderedList, or given that it's ordered, it will stop filtering after it reaches the first false condition, i.e., integer >= 3, or does it always check all items?

If it checks all items, is there a smarter way to filter items in a situation with an ordered list?

java performance java-stream