title: Pagination in List Java Spring
body: <p>I'm working on a Spring project where I have Tasks (id, name) and Assignments (task_id, name, ...). When I get the tasks I also get assigned task, but I was not able to implement a pagination system.</p>
  <pre class="lang-java prettyprint-override"><code>public List&lt;Tasks&gt; getAllTasks() {
         List&lt;Tasks&gt; task = (List&lt;Tasks&gt;) taskRepository.findAll();
         return task;
     }
  </code></pre>
  <p>That's my current code. I want to display 10 items in each paginated side.</p>
  <p>I tried something like that:</p>
  <pre class="lang-java prettyprint-override"><code>   public Page&lt;Tasks&gt; getAllTasks(Long page, Long size){
         List&lt;Tasks&gt; tsk = (List&lt;Tasks&gt;) taskRepository.findAll();
       
         int start =  new PageRequest(page, size).getOffset();
         int end = (start + new PageRequest(page, size).getPageSize()) &gt; tsk.size() ? tsk.size() : (start + new PageRequest(page, size).getPageSize());
  
         return new PageImpl&lt;Tasks&gt;(tsk.subList(start, end), new PageRequest(page, size), tsk.size());
     }
  </code></pre>
  <p>but it's not working.</p>
  <p>My current JSON response is as follows:</p>
  <pre class="lang-java prettyprint-override"><code>{
     id: 1,
     name: First Task
     assignments: [ 
          {
            id: 1,
            assignment_name: firstassignment
          },
          {
            id: 2,
            assignment_name: secondassignment
          }]
  }
  </code></pre>
  <p>I also read that it doesn't need to be a List. I'm open for any ideas.</p>