如何使用java if else从JSON文件中删除重复的元素?
在我的代码中找到的start来自我的JSON文件。现在在我的JSON文件中,我有很多同名的“开始”。例如,在这个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);
我不想让它添加相同“奥迪”的2倍,否则我怎么写逻辑
多谢各位
# 1 楼答案
# 2 楼答案
`您可以使用arraylist检查值是否已插入到addVertex
比如:
`# 3 楼答案
您还可以使用HashMap代替数组列表
这将比线性检查ArrayList更有效
您只需检查HashMap中是否有开始(如“audi”),如果没有,您可以将其添加到图形和HashMap中,如果HashMap中已经有开始,您可以跳过将其添加到图形和HashMap中
我得到的结果是: