0

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?

Ciprian Oancea
  • 99
  • 1
  • 11
  • 1
    What errors do you have? Btw: you compare `String`s with `==`, dont do that. – f1sh Jun 24 '20 at 22:05
  • I had errors when I tried to do with streams (I don't know how to use lamba function...) Sorry for == But is not this the problem. – Ciprian Oancea Jun 24 '20 at 22:06
  • 1
    Ignore any possible solution with streams and do this manually. You never add your `ps` List to `productsPerStars`, why? – f1sh Jun 24 '20 at 22:12
  • I have to make a list of products, then for each rating I have to associate a list of products and I don't know how... – Ciprian Oancea Jun 24 '20 at 22:15
  • Oops: `r.getProductName() == p.getName()` doesn't do what you hope it does. [Use `r.getProductName().equals(p.getName())` instead](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Bohemian Jun 24 '20 at 23:19
  • Does `r.getNumStars()` return average rating? – Ken Bekov Jun 24 '20 at 23:42
  • `r.getNumStars()` returns the number of the stars for each product (not the average rating). – Ciprian Oancea Jun 24 '20 at 23:45
  • @OanceaCiprian what do you mean by an average then? based on what do you evaluate the average number for products? – Naman Jun 25 '20 at 03:49
  • if p1 has 3 and 5 stars, I must have {4.0 = [p1]} – Ciprian Oancea Jun 25 '20 at 08:06

1 Answers1

1

Try this:

SortedMap<Double, List<String>> productsPerStars = new TreeMap<>(
    ratings.stream().collect(groupingBy(Rating::getNumStars, Rating:: getProductName))
);
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    *r.getNumStars() returns the number of the stars for each product (not the average rating)* from a comment. – Naman Jun 25 '20 at 03:48