I'm learning Java 8 and I want rewrite usual java code with java Stream. Ho to do it better. My code:
public Set<Product> getProductsByFilter(Map<String, List<String>>filterParams) {
Set<Product> productsByBrand = new HashSet<Product>();
Set<Product> productsByCategory = new HashSet<Product>();
Set<String> criterias = filterParams.keySet();
if (criterias.contains("brand")) {
for (String brandName : filterParams.get("brand")) {
for (Product product : listOfProducts) {
if (brandName.equalsIgnoreCase(product.
getManufacturer())) {
productsByBrand.add(product);
}
}
}
}
if (criterias.contains("category")) {
for (String category : filterParams.get("category")) {
productsByCategory.addAll(this.
getProductsByCategory(category));
}
}
productsByCategory.retainAll(productsByBrand);
return productsByCategory;
}
I don't know how to correnct reformatting code in if (criterias.contains("brand")).
brandcriteria exists andcategorycriteria does not exist then returnempty. Why isproductsByBrandcollection ignored? Another point is, if you have access tolistOfProductsin brand filter loop, why not use that list incategoryfilter loop? You can just loop once