如何遍历列表并找出Java8中缺少的块?
我们有一个JSON响应,格式如下。我们需要高效地处理这个json,并记录一些缺少一些重要信息(如选项块)的项ID(至少出于调试目的)。包括下面的示例响应如何遍历列表并找出Java8中缺少的块?,java,collections,nested-loops,Java,Collections,Nested Loops,我们有一个JSON响应,格式如下。我们需要高效地处理这个json,并记录一些缺少一些重要信息(如选项块)的项ID(至少出于调试目的)。包括下面的示例响应 { "items": [ { "itemId": "xxx", "priceList": [ { "price": 0.0, "options": [
{
"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()))并成功了。在我看来,这肯定是优雅和可读性比我以前的方法。再次感谢!