Print the first, last, and in-between integers of a range Given our spec from Code Golf:


  Given two integers, output the two integers, and then the range between them.
  The order of the range must be the same as the input.
  
  Examples:

 Input        Output
 0,  5   -&gt;   [0, 5, 1, 2, 3, 4]
-3,  8   -&gt;   [-3, 8, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7]
 4,  4   -&gt;   [4, 4]
 4,  5   -&gt;   [4, 5]
 8,  2   -&gt;   [8, 2, 7, 6, 5, 4, 3]
-2, -7   -&gt;   [-2, -7, -3, -4, -5, -6]



Using our test driven approach :) 

import org.junit.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.*;

public class CodeGolfTest {

@Test
public void firstLastAndEverythingBetweenExampleOTo5() {

    final List&lt;Integer&gt; integers = CodeGolf.firstLastAndEverythingBetween(0, 5);
    assertThat(integers).isNotNull().hasSize(6).containsExactly(0,5,1,2,3,4);


}

@Test
public void firstLastAndEverythingBetweenExampleMinus3To8() {

    final List&lt;Integer&gt; integers = CodeGolf.firstLastAndEverythingBetween(-3, 8);
    assertThat(integers).isNotNull().hasSize(12).containsExactly(-3,8,-2,-1,0,1,2,3,4,5,6,7);


}

@Test
public void firstLastAndEverythingBetweenExample4To4() {

    final List&lt;Integer&gt; integers = CodeGolf.firstLastAndEverythingBetween(4, 4);
    assertThat(integers).isNotNull().hasSize(2).containsExactly(4,4);


}

@Test
public void firstLastAndEverythingBetweenExample4To5() {

    final List&lt;Integer&gt; integers = CodeGolf.firstLastAndEverythingBetween(4, 5);
    assertThat(integers).isNotNull().hasSize(2).containsExactly(4,5);


}

@Test
public void firstLastAndEverythingBetweenExample8To2() {

    final List&lt;Integer&gt; integers = CodeGolf.firstLastAndEverythingBetween(8, 2);
    assertThat(integers).isNotNull().hasSize(7).containsExactly(8,2,7,6,5,4,3);


}

@Test
public void firstLastAndEverythingBetweenExampleMinus2ToMinus7() {

    final List&lt;Integer&gt; integers = CodeGolf.firstLastAndEverythingBetween(-2, -7);
    assertThat(integers).isNotNull().hasSize(6).containsExactly(-2,-7,-3,-4,-5,-6);


}
}


Can we optimize further this code?

The implementation:

import java.util.ArrayList;
import java.util.List;

public final class CodeGolf {

    private CodeGolf() {
    }

    public static List&lt;Integer&gt; firstLastAndEverythingBetween(final int a, final int b) {

        if (a == b) {
            return addAB(a, b);
        }

        final List&lt;Integer&gt; result = addAB(a, b);
        int initial = getInitial(a, b);

        for (int n = 1; n &lt; Math.abs(b - a); n++) {
            result.add(initial);
            if (b &gt; a) {
                initial++;
            } else {
                initial--;
            }
        }

        return result;
    }

    private static int getInitial(int a, int b) {
        return (b &gt; a) ? (a + 1) : (a - 1);
    }

    private static List&lt;Integer&gt; addAB(int a, int b) {
        final List&lt;Integer&gt; result = new ArrayList&lt;&gt;();
        result.add(a);
        result.add(b);
        return result;
    }
}
As you ask for optimization:


You re-evaluate Math.abs() in each iteration of the loop, which is a constant value. Consider for(int n = 1, upperBound = Math.abs(b - a); n &lt; upperBound; n++) instead.
The same goes for if (b &gt; a) which is also constant. Maybe calculate an offset beforehand int offset = (b &gt; a) ? 1 : -1; and then just add the offset in the loop.
... and if you already calculate such an offset, you may also use it to replace getInitial() with initial = a + offset;