Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
    }
 }

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading