Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4117610
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:51:22+00:00 2026-05-20T22:51:22+00:00

I am passed a collection of objects (some Contact class in my case) and

  • 0

I am passed a collection of objects (some Contact class in my case) and need to return a page from that collection.
My code feels much longer than it needs to be. Am I missing some libraries that could perform that more elegantly than iterating over each element one at a time like I do below?

protected Collection<Contact> getPageOfContacts(
  Collection<Contact> contacts, int pageIndex, int pageSize) {
  if (pageIndex < 0 || pageSize <= 0
    || pageSize > contacts.size()) {
    return contacts;
  }
  int firstElement = pageIndex * pageSize;
  int lastElement = (pageIndex + 1) * pageSize - 1;
  Collection<Contact> pagedContacts = new ArrayList<Contact>();
  int index = -1;
  for (Contact contact : contacts) {
    index++;
    if (index < firstElement) {
      continue;
    }
    if (index > lastElement) {
      break;
    }
    pagedContacts.add(contact);
  }
  return pagedContacts;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:51:23+00:00Added an answer on May 20, 2026 at 10:51 pm

    You could use Guava Iterables.partition:

    protected <T> Collection<T> getPageOfContacts(
            Collection<T> contacts, int pageIndex, int pageSize) {
        return Lists.newArrayList(
            Iterables.partition(contacts, pageSize)).get(pageIndex);
    }
    

    A more complex version does not create all pages to pick the right one, but stops when the right page is found.

    protected <T> Collection<T> getPageOfContacts(
            Collection<T> contacts, int pageIndex, int pageSize) {
        Iterator<List<T>> partitions = Iterators.partition(contacts.iterator(), pageSize);
    
        for(int page = 0; page<pageSize && partitions.hasNext(); page++){
            List<T> partition = partitions.next();
            if(page == pageIndex) return partition;
        }
        return Collections. <T> emptyList(); //or fail
    }
    

    Update:

    Thanks to ColinD to point out that:

    Iterables.get(Iterables.partition(contacts, pageSize), pageIndex)
    

    is a simpler implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In some part of my code I am passed a collection of objects of
I am writing some code to generate an opml file from a list of
I am trying to serialize some Linq objects using this code. private byte[] GetSerializedObj(object
In the book Java Concurrency in Practice , Brian Goetz says that objects passed
I am working on an MVC project. I need to return an object that
Previously I used IEnumerable<T> type, if I passed collection as parameter of method. But
I passed string from controllerA (second item in tab bar) to controllerB (first item
When does the expression passed to DisplayFor and EditorFor need to use the model
I am being passed a set of querystring parameters within a Parameters class with
My image is passed into my program from a server and saved as a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.