Java streams perform different operations by multiple filters

I want to return different error messages minimum and maximum constraints on the values in a given list. I was hoping to use Java streams but i am having to filter the same list twice. Whereas using a for loop i would only have to go over the list once. Is there a better way to achieve this via streams?

My code looks something like this:

list.stream().filter(i -> i < 0).findAny().ifPresent(i -> System.out.println("below min"));

list.stream().filter(i -> i > 100).findAny().ifPresent(i -> System.out.println("above max"));

Using a for loop i can place two if conditions while traversing over the list once,

In actual implementation there are other constraints as well and I'm adding an error to a list, if it not already exists, depending on each violation.

Will i have to filter the list each time for each violation? Help would be appreciated!



Comments

Popular posts from this blog

Spring Elasticsearch Operations

Object oriented programming concepts (OOPs)

Spring Boot and Vaadin : Filtering rows in Vaadin Grid