Lets say I have Car
s with Feature
s, and I have been given a List
of features to look for in the Car
s available with me. Though I could have done that using for-loop, what are the suggestions for below? Is this production-worthy?
import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
public class LookupTest {
public static void main(String[] args) {
Feature ft11 = new Feature(1);
Feature ft12 = new Feature(2);
Feature ft13 = new Feature(4);
Car fer = new Car("Ferrari", ft11, ft12, ft13);
Feature ft21 = new Feature(1);
Feature ft22 = new Feature(2);
Feature ft23 = new Feature(8);
Car suz = new Car("Suzuki", ft21, ft22, ft23);
List<Car> cars = Arrays.asList(fer, suz);
List<Integer> lookForTypes = Arrays.asList(4); // look whether any car has feature-type 4
if (CollectionUtils.isNotEmpty(lookForTypes)) {
/* code block in question : start */
boolean atLeastOneTypeFound = cars
.parallelStream()
.anyMatch(holding -> holding.getAmounts()
.parallelStream()
.anyMatch(feature -> {
return lookForTypes.contains(feature.type);
}));
/* code block in question : end */
System.out.println("atLeastOneTypeFound=" + atLeastOneTypeFound);
}
}
}
class Car {
List<Feature> featureList;
String name;
public Car(String name, Feature... features) {
featureList = Arrays.asList(features);
this.name = name;
}
public List<Feature> getAmounts() {
return featureList;
}
public String getName() {
return name;
}
}
class Feature {
int type;
public Feature(int type) {
this.type = type;
}
public int getType() {
return type;
}
}