195. Building a dynamic predicate from a custom map of conditions
Let’s consider the Car
model and a List<Car>
denoted as cars
:
public class Car {
private final String brand;
private final String fuel;
private final int horsepower;
...
}
Also, let’s assume that we receive a Map
of conditions of type field : value, which could be used to build a dynamic Predicate
. An example of such a Map
is listed here:
Map<String, String> filtersMap = Map.of(
"brand", "Chevrolet",
"fuel", "diesel"
);
As you can see, we have a Map<String, String>
, so we are interested in an equals()
comparison. This is useful to start our development via the following Java enum
(we follow the logic from Problem 194):
enum PredicateBuilder {
EQUALS(String::equals);
...
Of course, we can add more operators, such as startsWith()
, endsWith()
, contains()
, and so on. Next, based on the experience gained in...