title: Java Paginated List
body: <p>I have 3 <code>List</code> of different kind of <code>Objects</code>.
  For example:</p>
  
  <p><code>List</code> <code>A</code> contains <code>64</code> elements.</p>
  
  <p><code>List</code> <code>B</code> contains <code>33</code> elements.</p>
  
  <p><code>List</code> <code>C</code> contains <code>515</code> elements.</p>
  
  <p>So in total, I have <code>612</code> elements.</p>
  
  <p>I want to make groups (<code>Pagination</code>) of <code>100</code> elements, for example, it would look like this:</p>
  
  <p>Page <code>1</code>: <code>List</code> <code>A</code>: <code>64</code> elements / <code>List</code> <code>B</code>: <code>33</code> elements / <code>List</code> <code>C</code>: <code>3</code> elements</p>
  
  <p>Page <code>2</code>: <code>List</code> <code>A</code>: <code>0</code> elements / <code>List</code> <code>B</code>: <code>0</code> elements / <code>List</code> <code>C</code>: <code>100</code> elements</p>
  
  <p>Page <code>3</code>: <code>List</code> <code>A</code>: <code>0</code> elements / <code>List</code> <code>B</code>: <code>0</code> elements / <code>List</code> <code>C</code>: <code>100</code> elements</p>
  
  <p>Page <code>4</code>: <code>List</code> <code>A</code>: <code>0</code> elements / <code>List</code> <code>B</code>: <code>0</code> elements / <code>List</code> <code>C</code>: <code>100</code> elements</p>
  
  <p>Page <code>5</code>: <code>List</code> <code>A</code>: <code>0</code> elements / <code>List</code> <code>B</code>: <code>0</code> elements / <code>List</code> <code>C</code>: <code>100</code> elements</p>
  
  <p>Page <code>6</code>: <code>List</code> <code>A</code>: <code>0</code> elements / <code>List</code> <code>B</code>: <code>0</code> elements / <code>List</code> <code>C</code>: <code>100</code> elements</p>
  
  <p>Page <code>7</code>: <code>List</code> <code>A</code>: <code>0</code> elements / <code>List</code> <code>B</code>: <code>0</code> elements / <code>List</code> <code>C</code>: <code>12</code> elements</p>
  
  <p>My idea is creating a <code>Map&lt;Integer, List&lt;List&gt;</code> where the <code>key</code> would be the <code>Page</code> and the <code>value</code> a <code>List</code> which contains 3 <code>Lists</code> (one for each <code>List</code> <code>A</code>, <code>B</code> or <code>C</code>).</p>
  
  <p>Here is my code:</p>
  
  <pre><code>            int totalPages = 0;
              int totalElements = listA.size() + listB.size() + listC.size();
              if(totalElements % PAGE_SIZE == 0) {
                  totalPages = totalElements / PAGE_SIZE;
              }else {
                  totalPages = (totalElements / PAGE_SIZE) + 1;
              }
  
  
              Map&lt;Integer, List&lt;List&lt;ParentObject&gt;&gt;&gt; paginatedMap = new HashMap&lt;Integer, List&lt;List&lt;ParentObject&gt;&gt;&gt;();
              for(int i=0; i&lt;totalPages; i++) {
                  List&lt;List&lt;ParentObject&gt;&gt; list = new LinkedList&lt;List&lt;ParentObject&gt;&gt;();
                  List&lt;ObjectA&gt; subListA = new LinkedList&lt;ObjectA&gt;();
                  List&lt;ObjectB&gt; subListB = new LinkedList&lt;ObjectB&gt;();
                  List&lt;ObjectC&gt; subListC = new LinkedList&lt;ObjectC&gt;();
                  int total = 0;
  
                  if(total &lt;= PAGE_SIZE) {
                      subListA.addAll(listA.subList(0, (PAGE_SIZE-total)-1));
                      listA.removeAll(listA.subList(0, (PAGE_SIZE-total)-1));
                      total = total + subListA.size();
                  }
  
                  if(total &lt;= PAGE_SIZE) {
                      subListB.addAll(listB.subList(0, (PAGE_SIZE-total)-1));
                      listB.removeAll(listB.subList(0, (PAGE_SIZE-total)-1));
                      total = total + subListB.size();
                  }
  
                  if(total &lt;= PAGE_SIZE) {
                      subListC.addAll(listC.subList(0, (PAGE_SIZE-total)-1));
                      listC.removeAll(listC.subList(0, (PAGE_SIZE-total)-1));
                      total = total + subListC.size();
                  }
  
                  list.add(subListA);
                  list.add(subListB);
                  list.add(subListC);
                  paginatedMap.put(i, list);
              }
  </code></pre>
  
  <p>where <code>PAGE_SIZE</code> is 100</p>
  
  <p>That's not working because of course I need to check how many elements each <code>list</code> contains before calling the <code>subList</code> <code>method</code>.</p>
  
  <p>I think I'm taking the wrong way, but I don't see another way to do it.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks!</p>
  
  <p>Finally I got it to work.
  Here is the code:</p>
  
  <pre><code>private Map&lt;Integer, List&lt;List&lt;MyObject&gt;&gt;&gt; paginateDataRequest(List&lt;List&lt;MyObject&gt;&gt; requestLists, double pageSize) {
      Map&lt;Integer, List&lt;List&lt;MyObject&gt;&gt;&gt; result = new LinkedHashMap&lt;Integer, List&lt;List&lt;MyObject&gt;&gt;&gt;();
      int totalElements = 0;
  
      //We calculate the total of the elements contained in the requestLists.
      for(List&lt;MyObject&gt; subList : requestLists) {
          if(subList != null) {
              totalElements += subList.size();
          }
      }
  
      //We round it up. The result Map will contain x pages with {pageSize} elements each one. For example, if the total amount of request is 101,
      //our Map will have 2 pages (100 elements + 1 element)
      int totalRequests = (int)Math.ceil(totalElements / pageSize);
  
      //We iterate over each page
      for(int i=0; i&lt;totalRequests; i++) {
          List&lt;List&lt;MyObject&gt;&gt; entry = new LinkedList&lt;List&lt;MyObject&gt;&gt;();
  
          int freeElements = (int)pageSize;
  
          for(List&lt;MyObject&gt; list : requestLists) {
              List&lt;MyObject&gt; subList = new LinkedList&lt;MyObject&gt;();
              if(freeElements &gt; 0) {
                  if(list.size() &gt; freeElements) {
                      subList.addAll(list.subList(0, freeElements));
                  }else {
                      subList.addAll(list);
                  }
                  //We update the left free elements
                  freeElements -= subList.size();
              }
              entry.add(subList);
              list.removeAll(subList);
  
          }
          //We add a new page to the result Map
          result.put(i, entry);
      }
  
      return result;
  }
  </code></pre>
  
  <p>Thanks everyone for helping!</p>