I have products and each product have a rating. What I tried is:
public SortedMap<Double, List<String>> getProductsPerStars () {
SortedMap<Double, List<String>> productsPerStars = new TreeMap<>();
List<String> ps = new ArrayList<>();
for(Product p : products.values()) {
for(Rating r : ratings) {
System.out.println(r.getNumStars() + " " + r.getProductName());
if(r.getProductName() == p.getName()) {
System.out.println(this.getStarsOfProduct(p.getName()));
ps.add(p.getName());
// productsPerStars.put(key, value)
}
}
}
System.out.println(ps);
return productsPerStars;
}
The method getProductsPerStars() gives a map that associates the average number of stars with the list of product names that have this average, with the average values sorted in descending order and the names of the products sorted alphabetically. Products without reviews are discarded.
The output must look like this:
{4.0=[p2], 2.0=[p0, p1]}
I think that is better to do this with streams... but I don't know how (because I have some errors...). Can you please help me?