我有一个Map,其中包含对象的名称和ID。从我的服务中,我得到一个List,其中每个JsonNode都包含一个名称,并应包含一个ID。我想使用Map(具有权威性)来验证与JsonNode中的名称相对应的ID。例如:
JsonNode Map
Mark 23 Mark 23
Chris 43 Chris 43
Ralph 21 Ralph 31
在这种情况下,前两个匹配,最后一个不匹配。我想遍历所有JsonNodes和Map并收集节点ID与Map ID不匹配的所有名称。
我可以使其与以下代码一起使用:
// stream all the nodes,filter the nodes with either no "id" or with an id <> expected,// collect into a list.
nodes.stream().filter(r -> {
String name = r.get("name").asText();
if (r.has("id")) {
Long nodeId = r.get("id").asText();
Long expectedId = map.get(name);
if (expectedId == null) {
return false;
}
if (nodeId != map.get(name)) {
return true;
} else {
return false;
}
} else { //no id in node
return true;
}
}).map(r-> r.get("$id").asText()).collect(Collectors.toList());
列表中最多不超过2000个JsonNode,通常更少(parallelStream是一个好方法吗?如果使用parallelStream,我将创建自己的ForkJoinPool以更好地控制池的大小。如果不是parallelStream,那么期货(如CompletableFutures)会是更好的选择吗?
谢谢