我正在尝试创建一个HashMap,HashMap所以我将来更容易访问它的元素,如下所示.
问题是它只重复while循环的最后一个元素而不是其余元素.
HashMap<String,String> result = new HashMap<>();
HashMap<Integer, HashMap<String,String>> fr = new HashMap<>();
int i = 0;
try {
ResultSet rq = qexec.execSelect();
// ResultSetFormatter.out(System.out, rq, query);
// get result from SPARQL query
while (rq.hasNext()) {
QuerySolution soln = rq.next();
id = soln.getLiteral("?id").getLexicalForm();
//...
result.put("id",id);
//...
if (soln.getLiteral("?wateruse") != null) {
wateruse = soln.getLiteral("?wateruse").getLexicalForm();
//...
result.put("wateruse",wateruse);
} else {
System.out.println("NO");
}
fr.put(i, result);
i++;
}
} finally {
qexec.close();
}
Run Code Online (Sandbox Code Playgroud)
这是结果应该是这样的:
John001
High usage …Run Code Online (Sandbox Code Playgroud)