java 流API -需要迭代列表并与Map比较,将结果写入不同的Map

jgwigjjp  于 2023-01-19  发布在  Java
关注(0)|答案(2)|浏览(107)

公共类产品入库请求{

private String productId;

private Integer requestedQuantity;

}
我有一份名单

requestList.add(new ProductInStockRequest("100", 5));
    requestList.add(new ProductInStockRequest("200", 11));
    requestList.add(new ProductInStockRequest("300", 33));
    requestList.add(new ProductInStockRequest("400", 55));

我有一个Map〈字符串,整数〉productInDbMap = new HashMap〈〉();

productInDbMap.put("100", 10);
    productInDbMap.put("200", 10);
    productInDbMap.put("300", 44);
    productInDbMap.put("400", 77);

我创建了新的Map〈String,String〉responseMap =新的HashMap〈〉();
我需要检查List中的每个元素,检查相关productId的数量是否足够,并在responseMap中写入结果
尝试做这样的事情:

requestList.stream().map(requestedItem -> {
        int quantity = productInDbMap.get(requestedItem.getProductId());

        if (quantity >= requestedItem.getRequestedQuantity()) {
            responseMap.put(requestedItem.getProductId(), "order-able");

        } else {
            int availableQuantity = quantity - requestedItem.getRequestedQuantity();
            String s = String.valueOf(availableQuantity);
            responseMap.put(requestedItem.getProductId(), s);
        }
        return responseMap;
    });

没有,请指示

nukf8bse

nukf8bse1#

你可以这样开始:

Map<String, String> responseMap = requestList.stream().map(requestedItem -> {
            int quantity = productInDbMap.get(requestedItem.getProductId());

            if (quantity >= requestedItem.getRequestedQuantity()) {
                return new AbstractMap.SimpleEntry<>(requestedItem.getProductId(), "order-able");
            } else {
                int availableQuantity = quantity - requestedItem.getRequestedQuantity();
                String s = String.valueOf(availableQuantity);
                return new AbstractMap.SimpleEntry<>(requestedItem.getProductId(), s);
            }
        }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

一般来说,不要在流或lambda内部修改任何东西,编译器会允许这种情况(如果修改变量,编译器会抱怨),但这是一种不好的做法,也是不安全的。

piah890a

piah890a2#

您可以直接采集到Map:

Map<String,String> responseMap = requestList.stream()
            .collect(Collectors.toMap(ProductInStockRequest::getProductId, requestedItem ->  {
            int quantity = productInDbMap.get(requestedItem.getProductId());

            if (quantity >= requestedItem.getRequestedQuantity()) {
                return"order-able";
            } else {
                int availableQuantity = quantity - requestedItem.getRequestedQuantity();
                return String.valueOf(availableQuantity);
            }
        }));

如果您使用forEach,您的方法将运行良好:

Map<String,String> responseMap = new HashMap<>()
requestList.stream()
            .forEach(requestedItem ->  {
                int quantity = productInDbMap.get(requestedItem.getProductId());

                if (quantity >= requestedItem.getRequestedQuantity()) {
                    responseMap.put(requestedItem.getProductId(), "order-able");

                } else {
                    int availableQuantity = quantity - requestedItem.getRequestedQuantity();
                    String s = String.valueOf(availableQuantity);
                    responseMap.put(requestedItem.getProductId(), s);
                }
        });

相关问题