title: Find groups of continuous integers in a list in Java
body: <p>I have a list of integers placed in order.
  I want to get groups of consecutive integers as arrays with 1st and last integer of each group.</p>
  
  <p>For example, for (2,3,4,5,8,10,11,12,15,16,17,18,25) I want to get a list with those arrays: [2,5] [8,8] [10,12] [15,18] [25,25]</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  
  public class MyRangesTest {
  
  
  public static void main(String[] args) {
      //create list of integers
      List&lt;Integer&gt; list=Arrays.asList(2,3,4,5,8,10,11,12,15,16,17,18,25);
      System.out.println("list:" + list);
  
  
          //create a list with integers where a new sequense of consecutive integers starts or ends
          List&lt;Integer&gt; sublistsStarsAndEnds= new ArrayList&lt;&gt;();
          sublistsStarsAndEnds.add(list.get(0));//1st line (always in sublistsStarsAndEnds list)
          for (int i=1; i&lt;list.size()-1; i++){
              if (list.get(i)&gt;1+list.get(i-1)){
                  sublistsStarsAndEnds.add(list.get(i-1));
                  sublistsStarsAndEnds.add(list.get(i));
              }
          }
          sublistsStarsAndEnds.add(list.get(list.size()-1));//last line (always in sublistsStarsAndEnds list)
          System.out.println("sublistsStarsAndEnds: " + sublistsStarsAndEnds);//present the result
  
  
          //create list with arrays that represents start and end of each subrange of consequent integers
          List&lt;Integer[]&gt; ranges= new ArrayList&lt;&gt;();
          for (int i=0; i&lt;sublistsStarsAndEnds.size()-1; i=i+2){
              Integer[] currentrange=new Integer[2];
              currentrange[0]=sublistsStarsAndEnds.get(i);
              currentrange[1]=sublistsStarsAndEnds.get(i+1);
              ranges.add(currentrange);//present the result
          }
  
          //present the result
          String rangestxt="";//create result text
          for (int i=0; i&lt;ranges.size(); i++){
              rangestxt=rangestxt+ranges.get(i)[0]+ " " + ranges.get(i)[1]+ "    ";
           }        
          System.out.println("ranges: " + rangestxt);//present the result
  
  
      }
  
  }
  </code></pre>
  
  <p>This code works in the general case for what I want but when the last sequence has only 1 integer it fails to get the right result.</p>
  
  <p>For example when using this list: (2,3,4,5,8,10,11,12,15,16,17,18,25) instead of getting the ranges [2,5] [8,8] [10,12] [15,18] [25,25] we get the ranges [2,5] [8,8] [10,12] [15,25].</p>
  
  <p>The problem is with the detection of where the ranges start or end. In my code those places are stored in the <code>sublistsStarsAndEnds</code> list. Here instead of getting [2, 5, 8, 8, 10, 12, 15, 15, 25, 25] we get [2, 5, 8, 8, 10, 12, 15, 25].
  I tried to correct the code but I without good results.</p>
  
  <p>Any suggestions please?</p>
  
  <p>P.S. Someone wanted to get the result I want and asked a question for Python here "<a href="https://stackoverflow.com/questions/2154249/identify-groups-of-continuous-numbers-in-a-list">Identify groups of continuous numbers in a list</a>
  But I don't know Python so I tried my own coding.</p>