Available in versions: Dev (3.20) | Latest (3.19) | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11 | 3.10

ResultQuery as Iterable

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

The org.jooq.ResultQuery type extends java.lang.Iterable, and as such, inherits the JDK's Iterable.forEach() method, as well as it being a possible foreach loop source:

// External iteration, fetching records eagerly
for (var r : create.select(BOOK.TITLE).from(BOOK))
    System.out.println(r.get(BOOK.TITLE));

// Internal iteration, fetching records lazily
create.select(BOOK.TITLE).from(BOOK).forEach(r -> {
    System.out.println(r.get(BOOK.TITLE));
});
Note that unlike when lazy fetching using the org.jooq.Cursor API, the external iteration style will eager-fetch the entire intermediate org.jooq.Result into memory as jOOQ has no control over the lifecycle of the java.util.Iterator, and the JDK doesn't know an java.lang.AutoCloseable iterator.

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo