197. Extending Stream with containsAll() and containsAny()
Let’s assume that we have the following code:
List<Car> cars = Arrays.asList(
new Car("Dacia", "diesel", 100),
new Car("Lexus", "gasoline", 300),
...
new Car("Ford", "electric", 200)
);
Car car1 = new Car("Lexus", "diesel", 300);
Car car2 = new Car("Ford", "electric", 80);
Car car3 = new Car("Chevrolet", "electric", 150);
List<Car> cars123 = List.of(car1, car2, car3);
Next, in the context of a stream pipeline, we want to check if cars
contains all/any of car1
, car2
, car3
, or cars123
.
The Stream API comes with a rich set of intermediate and final operations, but it doesn’t have a built-in containsAll()
/containsAny()
. So, it is our mission to provide the following final operations:
boolean contains(T item);
boolean containsAll(T... items);
boolean...