如何在嵌套列表上使用 groupingBy

Pra*_*sad 4 java collections java-8 java-stream groupingby

我面临一个棘手的情况,我必须在具有嵌套列表的对象上使用 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
}
Run Code Online (Sandbox Code Playgroud)
public class OrderLine {
  private final Product product;
  private final Integer quantity;

  //getters, setters, equals, hashcode omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)
public class Product {
  private final String name;
  private final BigDecimal price;

  //getters, setters, equals, hashcode omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)
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?*)
      );
    }
}
Run Code Online (Sandbox Code Playgroud)

Nam*_*man 6

查找三个最受欢迎的产品

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

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

此外,迭代此映射的条目,同时sortlimitN 个元素进行ing 和ing 将使您更接近您正在寻找的答案。


找到最有价值的客户

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

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

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

我会将实现留给findHighestValueOrder您,因为如果您知道如何使用mapflatMap,您只需要max在这些订单中找到价格或回退到某个默认值,例如BigDecimal.ZERO.