What's a simple way to trim this group of data? (Java)

Andrew Guo :

I'm practicing playing around with simple JSON data in Java and I've gotten a bit stuck.

So let's say I'm a baker and I have some data on my various products over the years. For, example I have three products: cake, bagels, and cookies. My data is collected over a yearly basis, so an example JSON data set could be something like:

[ {"name": "cake", "consumers": 200, "tastiness": 8.0}, {"name": "cake", "consumers": 220, "tastiness": 8.3}, {"name": "bagel", "consumers": 1000, "tastiness": 6.4}, {"name": "bagel", "consumers": 1200, "tastiness": 7.5}, {"name": "bagel", "consumers": 800, "tastiness": 5.7}, {"name": "cookie", "consumers": 500, "tastiness": 9.6} ]

As you can see, there are two entries of "cake" to signal cake being around for two years, three entries of "bagel" to signal bagels being around for three years, etc.

I'd like to condense this data to get the weighted average tastiness for each product. For example, the average tastiness for cake is (8.0*200 + 8.3*220)/(200+220) = 8.157, so I'd want my new set to contain a data entry with value ("cake", 8.157), in addition to the corresponding average tastiness for bagels and cookies.

Parsing through the JSON data and extracting the values I want is trivial, but what I'm struggling with is the best method to extract/condensing the data values with the same name and getting the average tastiness.

So far, I've considered making arrayLists for names, consumerNumbers, and tastiness, but I realized that might be a bit messy and inconvenient.

I'm currently considering making a separate class called "Product" with properties "name", "consumers", and "tastiness", and then creating a single arrayList<Product>. However, I'm stuck on how I'd best iterate through the arrayList of products and grab the Products with identical names and calculating the weighted average.

I am aware that there probably exists an incredibly easy and simple solution to my issue but it's just not coming to me at the moment, so any help would be greatly appreciated. Thanks :)

almac777 :

So you have multiple kinds of products and an arbitrary number of groups, that you want to work on.

Have you considered using a Map? You could use something like Map<String, List<Product>>

You can build the map by using Java 8 streams (stream the List<Product>) and grouping over the product name.

Finally, you can for-each over the keySet of the map and the List of Products in the List

Example:

   List<Product> products = buildProductsList();
   Map<String, List<Product>> productsGroupedByName = products.stream()
      .collect(Collectors.groupingBy(Product::getName));
   for(String name : productsGroupedByName.keySet()) {
      System.out.println("The name is: " + name);
      for (Product product : productsGroupedByName.get(name)) {
          // do something with the products
      }
   }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165096&siteId=1