You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Stream API对比List<Order>与List<Product>判断订单是否可履约

判断订单是否可履行的Stream API实现方案

要判断订单能不能履行,核心就是两个检查:

  • 订单里的每一种商品,库存里都有对应条目
  • 库存中对应商品的数量 ≥ 订单需求数量

第一步:补全实体类的getter方法

原代码里的Product和Order没有对外暴露属性的方法,Stream里没法访问name和amount,先补上:

class Product {
    private String name;
    private int amount;

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

    // 新增getter
    public String getName() {
        return name;
    }

    public int getAmount() {
        return amount;
    }
}

class Order {
    private String name;
    private int amount;

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

    // 新增getter
    public String getName() {
        return name;
    }

    public int getAmount() {
        return amount;
    }
}

方法一:直接用Stream遍历订单检查

用allMatch判断所有订单都满足条件,对每个订单,从库存列表里找到对应商品,再比对数量:

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));

        // 判断订单是否可履行
        boolean isFulfillable = orderList.stream()
                .allMatch(order -> 
                    // 查找库存中对应商品
                    productList.stream()
                            .filter(product -> product.getName().equals(order.getName()))
                            .findFirst()
                            // 存在且库存数量≥订单需求
                            .map(product -> product.getAmount() >= order.getAmount())
                            // 找不到对应商品则返回false
                            .orElse(false)
                );

        System.out.println("订单是否可履行:" + isFulfillable); // 输出false,因为juice和milk库存不足
    }
}

方法二:转Map优化查询效率

如果库存商品数量多,每次遍历List找商品效率低,先把Product转成Map<String, Integer>(商品名对应库存数量),再用Stream判断:

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));

        // 把库存转成商品名-库存数的Map
        Map<String, Integer> stockMap = productList.stream()
                .collect(Collectors.toMap(Product::getName, Product::getAmount));

        // 判断订单是否可履行
        boolean isFulfillable = orderList.stream()
                .allMatch(order -> {
                    Integer stockAmount = stockMap.get(order.getName());
                    // 商品存在且库存足够
                    return stockAmount != null && stockAmount >= order.getAmount();
                });

        System.out.println("订单是否可履行:" + isFulfillable); // 输出false
    }
}

逻辑说明

  • allMatch会遍历所有订单,只有当所有元素都满足条件时才返回true
  • 方法二中的Map查询是O(1)时间复杂度,比方法一的O(n²)更高效,适合大规模数据场景

内容的提问来源于stack exchange,提问作者Kto Tam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 12:42:18