---
title: "Pagination"
description: "One page, every page, or one item at a time."
url: "https://openemail.uk/docs/java/pagination"
area: "Java"
category: "Getting started"
---

# Pagination

One page, every page, or one item at a time.

## One page

**OnePage.java**

```
RequestOptions inbox = RequestOptions.of("folder", "inbox").limit(25);
Page page = client.threads().list(inbox);

for (Map<String, Object> thread : page) {
    System.out.println(thread.get("id") + " " + thread.get("subject"));
}

if (page.hasMore()) {
    Page next = client.threads().list(inbox.cursor(page.nextCursor()));

    System.out.println(next.size());
}
```

A `list` method returns a `Page` with `items()`, `hasMore()` and `nextCursor()`, and a `for` loop walks its items. Pass `nextCursor()` back as `cursor`, with the same filters, to read the next page.

## Every page

**EveryPage.java**

```
List<Map<String, Object>> contacts = client.contacts().listAll();

System.out.println(contacts.size());
```

A `listAll` method follows the cursor to the end and returns every item in one `List`. A failure on any page fails the whole call.

## One item at a time

**Iterate.java**

```
for (Map<String, Object> thread : client.threads().iterate(RequestOptions.of("query", "invoice"))) {
    if (Boolean.TRUE.equals(thread.get("hasUnread"))) {
        System.out.println(thread.get("id"));

        break;
    }
}

long starred = client.threads().iterate(RequestOptions.of("labelIds", "STARRED")).stream().count();

System.out.println(starred);
```

An `iterate` method returns a `PagedIterable`, which asks for the next page only when the loop reaches it, so a `break` stops the requests. `stream()` gives the same walk as a `Stream`, and `toList()` collects it.

> An iterator stops when the API repeats a cursor it has already followed, so a walk can never loop for ever.
