Skip to content
Matthew Dean edited this page Mar 21, 2015 · 8 revisions

Iteration through any sort of data returned by the Reddit API is done through the Paginator class. Paginator is used to iterate a listing, a data type defined by the Reddit API (see here for more). Its respective class can be found here.

Perhaps the most simplistic way to demonstrate a Paginator is by using SubredditPaginator. You can use this class to iterate the submissions on the front page or a subreddit.

RedditClient reddit = new RedditClient("MY-USER-AGENT");
SubredditPaginator frontPage = new SubredditPaginator(reddit);
// Adjust the request parameters
frontPage.setLimit(50);                    // Default is 25 (Paginator.DEFAULT_LIMIT)
frontPage.setTimePeriod(TimePeriod.MONTH); // Default is DAY (Paginator.DEFAULT_TIME_PERIOD)
frontPage.setSorting(Sorting.TOP);         // Default is HOT (Paginator.DEFAULT_SORTING)
// This Paginator is now set up to retrieve the highest-scoring links submitted within the past
// month, 50 at a time

// Since Paginator implements Iterator, you can use it just how you would expect to, using next() and hasNext()
Listing<Submission> submissions = frontPage.next();
for (Submission s : submissions) {
    // Print some basic stats about the posts
    System.out.printf("[/r/%s - %s karma] %s\n", s.getSubredditName(), s.getScore(), s.getTitle());
}

The output for this example might look something like this:

[/r/pics - 8510 karma] Got divorced, lost my job, so me and my buddy got on our motorcycles and rode North to the Alaskan Arctic until the road ran out.
[/r/funny - 6560 karma] Man trying to return a dog's toy gets tricked into playing fetch
[/r/videos - 6557 karma] Man races train overground from one station to the next.
(...)

Paginator and its subclasses can found in the net.dean.jraw.paginators package.

Clone this wiki locally