如何检查节点是否已存在

问题描述 投票:0回答:1

我想在添加新节点之前检查节点中是否已存在该节点。

我尝试使用foreach循环来做它。但它没有用。

boolean returnVal = false;
         for (Node node : displayGraph) {
             if (node.getId().equals(n.getId())){
                 returnVal = true;
             }
             else{
                 returnVal =false;
             }
         }

如果节点已存在于图表上,我想检索true

graph-algorithm graphstream
1个回答
0
投票

只需检查Graph.getNode(String)返回的内容。如果不存在具有该字符串id的节点,则返回null

boolean returnVal = displayGraph.getNode(n.getId()) == null ? false : true;
© www.soinside.com 2019 - 2024. All rights reserved.