4

我面临一个棘手的情况,我必须在具有嵌套列表的对象上使用 groupingBy。我用 map()、flatmap()、toMap() 尝试了一些东西,但无法提出解决方案,只能绕圈子。非常感谢流专家的任何帮助。我需要在我的 OrdersAnalyzer 类中实现 2 个方法。这是我的对象的样子:

public class Order {
  private final String id;
  private final Set<OrderLine> orderLines = new HashSet<>();
  private final Customer customer;

  //getters, setters, equals, hashcode omitted for brevity
}
public class OrderLine {
  private final Product product;
  private final Integer quantity;

  //getters, setters, equals, hashcode omitted for brevity
}
public class Product {
  private final String name;
  private final BigDecimal price;

  //getters, setters, equals, hashcode omitted for brevity
}
public class OrdersAnalyzer {
    /**
     * Should return at most three most popular products. Most popular product is the product that have the most occurrences
     * in given orders (ignoring product quantity).
     * If two products have the same popularity, then products should be ordered by name
     *
     * @param orders orders stream
     * @return list with up to three most popular products
     */
    public List<Product> findThreeMostPopularProducts(Stream<Order> orders) {
      orders.forEach(order -> {
        order.getOrderLines().stream().collect(
          Collectors.groupingBy(OrderLine::getProduct, *What to add here?* )
        );
      });
    }

    /**
     * Should return the most valuable customer, that is the customer that has the highest value of all placed orders.
     * If two customers have the same orders value, then any of them should be returned.
     *
     * @param orders orders stream
     * @return Optional of most valuable customer
     */
    public Optional<Customer> findMostValuableCustomer(Stream<Order> orders) {
      orders.collect(
        Collectors.groupingBy(Order::getCustomer, *What to add here?*)
      );
    }
}
4

1 回答 1

6

找三个最受欢迎的产品

您希望支持的 API 需要您创建产品频率图,以便在找到top-N. 在这种情况下Collectors.counting()将是一个很好downstream的利用。

请注意,它会为您提供Map<Product, Long> productFrequency结果,然后您需要定义一个自定义Comparator<Map.Entry<Product, Long>>辅助,并对名称比较进行额外检查。

此外,遍历此映射的条目,同时sorting 和limiting 到 N 个元素将使您更接近您正在寻找的答案。


找到最有价值的客户

在此 API 中,您希望根据客户各自订单中的最高值来比较客户。toList因此,使用默认下游分组就足够了。

它将帮助您Map<Customer, List<Order>>,其中您需要max从每个客户的所有订单中找到具有价格值的条目。

因此,比较器如Comparator.comparing(e -> findHighestValueOrder(e.getValue()), 其中e.getValue()aList<Order> customerOrders将帮助您解决问题。

我将由findHighestValueOrder您来执行,因为如果您知道如何mapflatMap,您只需要max在这些订单中找到价格或回退到一些默认值,例如BigDecimal.ZERO

于 2021-05-03T16:07:55.900 回答