在我的代码中找到的这个开始来 self 的 JSON 文件。现在,在我的 JSON 文件中,我有很多同名的“start”。例如,在此 JSON 文件中,我在开头有重复项(内容均为“audi”):
{"rel": "AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "0209765bf7185615ef242513f61ca0f61efe2a04", "features": ["audi AtLocation -", "audi - curitiba", "- AtLocation curitiba"], "end": "curitiba", "license": "/l/CC/By-SA", "uri": "/a/[AtLocation/,audi/,curitiba/]", "start": "audi", "context": "/ctx/all", "surfaceText": null}
{"rel": "AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "5158d1cfce728efb3e42840d166ec06153a3d77d", "features": ["audi AtLocation -", "audi - ingolstadt", "- AtLocation ingolstadt"], "end": "unite_state", "license": "/l/CC/By-SA", "uri": "/a/[AtLocation/,audi/,ingolstadt/]", "start": "audi", "context": "/ctx/all", "surfaceText": null}
这是我的 Java 代码:
String start = (String) jsonObject.get("start");
StartVertex startV = new StartVertex(start);
helloGraphDB.addVertex(startV);
我不希望它添加同一个“audi”的Vertex 2次,我该如何编写逻辑if else out?
谢谢。
最佳答案
`您可以使用arraylist检查值是否已经插入到addVertex
喜欢:
ArrayList addedVertex_Val = new ArrayList();`String start = (String) jsonObject.get("start"); boolean shouldInsert = true; for (int al_length = 0; al_length < addedVertex_val.size(); al_length++) { String val = addedVertex_val.get(al_length).toString(); if (val.equals(start)) { shouldInsert = false; break; } } if (shouldInsert) { StartVertex startV = new StartVertex(start); helloGraphDB.addVertex(startV); addedVertex_Val.add(startV); }
关于java - if else 如何使用 java 从 JSON 文件中删除重复元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12598713/