If you need to preserve order you can represent the list as an RDF Collection. If you have a list of strings, e.g.:
List<String> objects = Arrays.asList("o1", "o2", "o3");
representing it as an RDF collection would look something like this (in Turtle syntax):
_:node a rdf:List;
rdf:first "o1" ;
rdf:rest [
rdf:first "o2";
rdf:rest [
rdf:first "o3";
rdf:rest rdf:nil .
]
].
That may look cumbersome, but the RDF4J libraries offer some RDF Collection utilities to make handling this easier from code. To transform your Java list to an RDF Collection, you can do something like this:
// starting node of the RDF collection
Resource head = valueFactory.createBNode();
// convert our list and add it to a newly-created Model
Model collection = RDFCollections.asRDF(objects, head, new LinkedHashModel());
Here, collection
is just an RDF model (a set of RDF statements) that you can add to your triplestore like any other RDF data (see the RDF4J docs for some further tips on how to work with collections when storing and retrieving from a repository).
To convert a RDF collection back to a Java list (preserving order):
List<Value> values = RDFCollections.asValues(collection, head, new ArrayList<Value>());
Or if you want them back as a list of string objects again:
List<String> values = new ArrayList<>();
RDFCollections.consumeValues(collection, head, v -> values.add(v.stringValue()));
Edit it is also possible to retrieve such RDF collection items in order using SPARQL 1.1, though it is not straightforward. See Is it possible to get the position of an element in an RDF Collection in SPARQL? for a good solution.
All that being said though, it's probably a good idea to stop and think if you actually need to store things in a fixed order. RDF is by its nature unordered, and while RDF Collection modelling provide a mechanism, it's not efficient to store everything as collections.
In most common cases, the order is not really part of the actual data model, but is a presentation issue. In those cases, you can most of the time use a SPARQL query with an ORDER BY
clause instead, to sort the query results.