使用 java 使用大数据数组写入 json 文件
writing json file using arrays with large data using java
我正在尝试构建一个 json 文件来获取自动完成控制的文本框。
该文件将包含数百万个元素,因此我试图在节省内存和时间的同时消除重复项。由于我使用的是数组,因此对于少量以下代码仍然有效,随着数组变大,执行速度变得非常慢。
int i = 0;
JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
while (iter.hasNext()) {
Map<String,String>forJson = new HashMap<String, String>();
Statement stmt = iter.nextStatement();
object = stmt.getObject();
forJson.put("key", object.asResource().getLocalName());
forJson.put("value", object.asResource().getURI());
i++;
System.out.println(i);
if(!array.contains(forJson))
{
array.add(forJson);
}
}
obj.put("objects", array);
FileWriter file = new FileWriter("/homeDir/data.json");
file.write(obj.toJSONString());
file.flush();
file.close();
array.contains 控件消除了重复项,但它对执行时间有相当大的负面影响。
json 文件应该有类似
的标记
[{"key": "exampleText1", "value": "exampleValue1"},
{"key": "exampleText2", "value": "exampleValue2"}]
使用 HashSet 来包含您已经添加的键:
...
Set<String> usedKeys = new HashSet<String>();
while (iter.hasNext()) {
Map<String,String>forJson = new HashMap<String, String>();
Statement stmt = iter.nextStatement();
object = stmt.getObject();
String key = object.asResource().getLocalName();
if(!usedKeys.contains(key)) {
usedKeys.add(key);
forJson.put("key", key);
forJson.put("value", object.asResource().getURI());
array.add(forJson);
}
i++;
System.out.println(i);
}
如果您需要进行唯一性检查以包含该值,您可以使用您知道键中不能存在的字符分隔符附加这两个值。例如:
String key = object.asResource().getLocalName();
String value = object.asResource().getURI();
String unique = key + "|@|@|" + value;
if(!usedKeys.contains(unique)) {
usedKeys.add(unique);
forJson.put("key", key);
forJson.put("value", value);
array.add(forJson);
}
我正在尝试构建一个 json 文件来获取自动完成控制的文本框。
该文件将包含数百万个元素,因此我试图在节省内存和时间的同时消除重复项。由于我使用的是数组,因此对于少量以下代码仍然有效,随着数组变大,执行速度变得非常慢。
int i = 0;
JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
while (iter.hasNext()) {
Map<String,String>forJson = new HashMap<String, String>();
Statement stmt = iter.nextStatement();
object = stmt.getObject();
forJson.put("key", object.asResource().getLocalName());
forJson.put("value", object.asResource().getURI());
i++;
System.out.println(i);
if(!array.contains(forJson))
{
array.add(forJson);
}
}
obj.put("objects", array);
FileWriter file = new FileWriter("/homeDir/data.json");
file.write(obj.toJSONString());
file.flush();
file.close();
array.contains 控件消除了重复项,但它对执行时间有相当大的负面影响。
json 文件应该有类似
的标记[{"key": "exampleText1", "value": "exampleValue1"},
{"key": "exampleText2", "value": "exampleValue2"}]
使用 HashSet 来包含您已经添加的键:
...
Set<String> usedKeys = new HashSet<String>();
while (iter.hasNext()) {
Map<String,String>forJson = new HashMap<String, String>();
Statement stmt = iter.nextStatement();
object = stmt.getObject();
String key = object.asResource().getLocalName();
if(!usedKeys.contains(key)) {
usedKeys.add(key);
forJson.put("key", key);
forJson.put("value", object.asResource().getURI());
array.add(forJson);
}
i++;
System.out.println(i);
}
如果您需要进行唯一性检查以包含该值,您可以使用您知道键中不能存在的字符分隔符附加这两个值。例如:
String key = object.asResource().getLocalName();
String value = object.asResource().getURI();
String unique = key + "|@|@|" + value;
if(!usedKeys.contains(unique)) {
usedKeys.add(unique);
forJson.put("key", key);
forJson.put("value", value);
array.add(forJson);
}