If you can use the Java 8 constructors, another approach you can use is threads and predicates. You can get a representation of the ArrayList stream, apply a predicate to it, and then get a counter:
public int howManyBetweenTheseYears(int startYear, int endYear) {
return inventory.stream()
.filter((Lamborghini year) -> (modelYear >= startYear) && (modelYear <= endYear))
.count();
}
What this means is to get a set of Lamborghini objects from the inventory that matches your condition using the method filter, and then return the number of matching items.
source
share