java 使用StreamAPI比较列表< Order>与列表< Product>

kse8i1jr  于 2023-04-10  发布在  Java
关注(0)|答案(2)|浏览(123)

有必要确定是否可以履行此订单:库存中每种产品是否足够。我不知道如何使用流比较列表

class Product {
    private String name;
    private int amount;

    public Product(String name, int amount) {
        this.name = name;
        this.amount = amount;
    }
}
class Order {
    private String name;
    private int amount;

    public Order(String name, int amount) {
        this.name = name;
        this.amount = amount;
    }
}
class TestWarehouse {

    public static void main(String[] args) {

        Product apple = new Product("apple", 3);
        Product juice = new Product("juice", 2);
        Product milk = new Product("milk", 4);

        Order order1 = new Order("apple", 3);
        Order order2 = new Order("juice", 3);
        Order order3 = new Order("milk", 5);

        List<Order> orderList = new ArrayList<>(List.of(order1, order2, order3));
        List<Product> productList = new ArrayList<>(List.of(apple, juice, milk));
//        orderList.stream()
//                .filter()
//                .anyMatch()
    }
}
kiayqfof

kiayqfof1#

您可以首先创建一个Map,其中包含每个产品的金额Collectors.groupingBy。然后,迭代所有订单并比较金额。

Map<String, Integer> freq = productList.stream().collect(
    Collectors.groupingBy(Product::getName, Collectors.summingInt(Product::getAmount)));
orderList.stream().forEach(order -> {
    boolean works = freq.getOrDefault(order.getName(), 0) >= order.getAmount();
    System.out.println(works); // or do anything else
});

或者,如果productList中的每个Product都有一个唯一的名称,那么使用Collectors.toMap更简单。

Map<String, Integer> freq = productList.stream().collect(
          Collectors.toMap(Product::getName, Product::getAmount));
r3i60tvu

r3i60tvu2#

您可以使用anyMatch检查orderList中是否有任何元素与自定义 predicate 定义的条件相匹配。
下面是一个例子:

class TestWarehouse {
    
        public static void main(String[] args) {
    
            Product apple = new Product("apple", 3);
            Product juice = new Product("juice", 2);
            Product milk = new Product("milk", 4);
    
            Order order1 = new Order("apple", 3);
            Order order2 = new Order("juice", 3);
            Order order3 = new Order("milk", 5);
    
            List<Order> orderList = new ArrayList<>(List.of(order1, order2, order3));
            List<Product> productList = new ArrayList<>(List.of(apple, juice, milk));
    
            // Compare List<Order> with List<Product> using StreamAPI
            boolean anyMatch = orderList.stream()
                    .anyMatch(order -> productList.stream()
                            .anyMatch(product -> product.getName().equals(order.getName()) && product.getAmount() >= order.getAmount()));
    
            System.out.println("Any match: " + anyMatch);
        }
    }

在这个例子中,我假设您有getName()getAmount()方法。
如果你想检查orderList中的所有订单是否都可以用productList中的产品完成,你可以使用allMatch()方法而不是anyMatch()
下面是更新后的代码:

boolean allMatch = orderList.stream()
        .allMatch(order -> productList.stream()
                .anyMatch(product -> product.getName().equals(order.getName()) && product.getAmount() >= order.getAmount()));

System.out.println("All match: " + allMatch);

以下是一些资源:

相关问题