Question: Lets take a look at my program. That have some imaginary .txt file with car models.

```
public class Car {
    private String model;

    public String getModel() {
        return this.model;
    }
}

public class Main {
    public static void Main(String[] args) {
        ArrayList<Car> cars = loadCars(path); //imaginary func and file
        String someModel = generateSomeModel(); //imaginart func

        for (Car c : cars) 
            if (c.getModel.equals(someModel))
                System.out.println("Hit!");

        for (Car c : cars) 
            if (!c.getModel.equals(someModel))
                System.out.println("Hit!");

    }
}

```

Lets suppose that I have to go through my cars list a 100 times and compare them in if statement for some different attributes every time, that means I will have to write 100 different for loops that will be 90% same. Is there a way to write a function that will take as a parametar Boolean equation? Something like this.

```
public static void printCars(Boolean equation) {
    for (Car c : cars) 
        if (Boolean equation)
            System.out.println("Hit!");
}

```

Answer: 
You can use Predicate interface to pass a function (or lambda) as parameter to your function:

<https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html>

With a predicate you can rewrite your function in a following way:

```
public static void printCars(Predicate<Car> filter) {
    for (Car c : cars) 
        if (filter.test(car))
            System.out.println("Hit!");
}

```

And then pass required function in the following way:

`printCars(c -> c.getModel().equals("bmw"));`
In case you need to compare you cars with multiple attributes, you need just to override **equals** method for *Car* class.

```
@Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof Car) {
            Car anotherCar = (Car)obj;
            return this.model.equals(anotherCar.model) &&
                    this.color.equals(anotherCar.color) &&
                    this.year == anotherCar.year;
        }
        return false;
    }

```

This way you will compare all necessary attributes in one step:

```
List<Car> cars = //cars list
Car someCar = //bmw car
for (Car c : cars) {
    if (c.equals(someCar)) {
        System.out.println("Hit!");
    } else {
        System.out.println("Another Hit!");
    }
}

```