Take the 2-minute tour ×

My question is similar to the one asked here: http://forum.springsource.org/showthread.php?84508-jdbctemplate.query()-sorted-result-set but a clear answer was not provided - an ArrayList does not guarantee order.

Basically, I would like to know if the call returned by jdbcTemplate.query() guarantees the order of the resultset and if I can dump it into a LinkedList and pass it along :)


Edit: I should clarify that the query does include an order by clause, hence my requirements for a resultset that guarantees order. I was incorrect regarding the ArrayList not doing so. Since the jdbcTemplate is an interface the implementation would depend upon the db library. Should I make the assumption that an ArrayList will be used or sort it again to be on the safe side?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

an ArrayList does not guarantee order.

This is just wrong. An ArrayList does guarantee order. It has a get(int index)' method which can be used to retrieve an element from the specified 0-based index. Theadd(T item)` method will add them in sequence to the list. You may be confusing the List collection type with the Set interface, which is similar to a List except for it does not guarantee order.

That said, it does not actually answer your question...

The ResultSet, without a specified ordering, will return the data in the table's natural order. This is usually based of of the PK field(s) but this is not true for all DBMSs.

If order is important, specify it to be certain. Even if it comes back in the desired order now, changes to the DB schema might affect this later and break assumptions made by your code. Being explicit is better, and will express the code's intent clearer.

Update from edit to question: If an order by is specified in the query, you can rely 100% on the order of the result set. No need to sort it again.

share|improve this answer
Agreed, an explicit order by clause in your query is probably what you want. –  Kyri Sarantakos Jun 7 '11 at 19:20
Thanks! That's what I was looking for :) –  Victor Parmar Jun 7 '11 at 21:01

Does this answer your question?

public List<T> extractData(ResultSet rs) throws SQLException {
    List<T> results = (this.rowsExpected > 0 ? new ArrayList<T>(this.rowsExpected) : new ArrayList<T>());
    int rowNum = 0;
    while (rs.next()) {
        results.add(this.rowMapper.mapRow(rs, rowNum++));
    return results;

From RowMapperResultSetExtractor. This class is used in in several query() methods in JdbcTemplate.

No place for result set records mixing. In fact, result set is more like an iterator or stream rather than a full collection. This means that it would be very inefficient to change the order because you would have to load all the records from result set in advance.

share|improve this answer

Its not required to sort records again if you are using order by condtion in sql, check your stored procedure cursor output query " OPEN o_provisionalOrders FOR SELECT cols from table order by condtion " is proper.

share|improve this answer

Your Answer


