title: Splice arraylist in Java and group the second part
body: <p>I have a List of Dto, composed by 'userName' and 'skills'. For example:
  userName: "John", skills: 5</p>
  
  <p>If there are more than 5 elements (so 5 users) I'd like to maintaining the top five, and grouping the other under one name ('Others') and under one skill (the sum of all the skills). I thought of using the sublist method, create two temporary array, group the dto of the second array and merge all in the main Arraylist. 
  This is my code but it doesn't work. Why?</p>
  
  <pre><code>final List&lt;MyDto&gt; usersData = mydao.getUsers();
  List&lt;MyDto&gt; result = new ArrayList&lt;MyDto&gt;();    
  
  
  if (usersData != null &amp;&amp; usersData.size() &gt; 0) {
      List&lt;MyDto&gt; tmpFirstFiveUsers = new ArrayList&lt;MyDto&gt;();
  
      List&lt;MyDto&gt; tmpOthers = new ArrayList&lt;MyDto&gt;();
  
  
      if(usersData.size() &gt; 5) {
          // Save first 5 users
          tmpFirstFiveUsers = usersData.subList(0, 5);
  
          // Save others
          tmpOthers = usersData.subList(5, usersData.size());
  
          // Sum of 'others' skill
          int skills = 0;
          for (MyDto other : tmpOthers) {
              skills += other.getSkills();
          }
  
          String other = "Altro";
  
          MyDto emptyDto = new MyDto();
          emptyDto.setUserName(other);
          emptyDto.setSkills(skills);
  
          tmpOthers.clear();
  
          tmpOthers.add(emptyDto);
        } 
  
          result.clear();
  
          result.addAll(tmpFirstFiveUsers);
          result.addAll(tmpOthers);
  </code></pre>
  
  <p>UPDATE:
  I can't do correctly the sublist, it doesn't work.
  I give also an example to understand:
  usersData:</p>
  
  <ul>
  <li>userName: "a", skills: "2"</li>
  <li>userName: "b", skills: "1"</li>
  <li>userName: "c", skills: "5"</li>
  <li>userName: "d", skills: "5"</li>
  <li>userName: "e", skills: "5"</li>
  <li>userName: "f", skills: "5"</li>
  <li>userName: "g", skills: "5"</li>
  <li>userName: "h", skills: "5"</li>
  <li>userName: "i", skills: "5"</li>
  </ul>
  
  <p>What I expected is:
  usersData:</p>
  
  <ul>
  <li>userName: "a", skills: "2"</li>
  <li>userName: "b", skills: "1"</li>
  <li>userName: "c", skills: "5"</li>
  <li>userName: "d", skills: "5"</li>
  <li>userName: "e", skills: "5"</li>
  <li>userName: "Others", skills: "20"</li>
  </ul>