check each object, return true when the first condition is met, else return false

I have a method that works fine. This is how it looks.

private ArrayList<Car> carsInStore ; //in car class have setter/getter/quals/Constructor.

public boolean checkIfcarInStore(Car c) {   
    for (Car car : carsInStore) {
        if(car.equals(c)){
            return true;
        }
    }}

I wanna switch this to Lambda. but I am not sure how fill in the if (condition) return true or return false outside.
and i know i can do it in stream too. Can anyone give an example?

>Solution :

If you really want to use lambda, this should work else you have your answer within your comment.


private ArrayList<Car> carsInStore ;

public boolean checkIfcarInStore(Car c) {   
    return carsInStore.stream().anyMatch(c::equals);
    }
 }

Leave a Reply