Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何遍历列表并找出Java8中缺少的块?_Java_Collections_Nested Loops - Fatal编程技术网

如何遍历列表并找出Java8中缺少的块?

如何遍历列表并找出Java8中缺少的块?,java,collections,nested-loops,Java,Collections,Nested Loops,我们有一个JSON响应,格式如下。我们需要高效地处理这个json,并记录一些缺少一些重要信息(如选项块)的项ID(至少出于调试目的)。包括下面的示例响应 { "items": [ { "itemId": "xxx", "priceList": [ { "price": 0.0, "options": [

我们有一个JSON响应,格式如下。我们需要高效地处理这个json,并记录一些缺少一些重要信息(如选项块)的项ID(至少出于调试目的)。包括下面的示例响应

{
    "items": [
        {
            "itemId": "xxx",
            "priceList": [
                {
                    "price": 0.0,
                    "options": [
                        {
                            "price": 0.0,
                            "priceType": "yyy"
                        }
                    ]
                }
            ]
        },
        {
            "itemId": "xxx",
            "priceList": [
                {
                    "price": 0.0
                }
            ]
        },
        {
            "itemId": "xxx",
            "priceList": [
                {
                    "price": 0.0,
                    "options": [
                        {
                            "price": 0.0,
                            "priceType": "yyy"
                        }
                    ]
                }
            ]
        },
        {
            "itemId": "xxx",
            "priceList": [
                {
                    "price": 0.0
                }
            ]
        }
    ]
}
我能够记录丢失的项目ID,如下所示

items.forEach(item -> {
if (CollectionUtils.isEmpty(item.getUoms().get(0).getPricingOptions())) {
    log.info("price options is missing for item {} ", item.getItemId());
}
}))

有没有更好/更快的方法


谢谢

我不确定您是否可以更好地调用它,但您可以使用streams来实现这一点:

items.stream()
    .filter(it -> it.getUoms().get(0).getPricingOptions().isEmpty())
    .forEach(it -> log.info("price options is missing for item {} ", it.getItemId()));

谢谢你的快速回复。我得到了一个NPE与上述变化。但是我将代码稍微修改为filter(item->CollectionUtils.isEmpty(item.getPriceList().get(0.getOptions()))并成功了。在我看来,这肯定是优雅和可读性比我以前的方法。再次感谢!