There should be a short and sweet way to generate a List<Integer> or possibly Integer[] or int[] , with sequential values ββfrom some start value to an end value.
That is, the shorter, but equivalent to 1 of the following:
void List<Integer> makeSequence(int begin, int end) { List<Integer> ret = new ArrayList<>(end - begin + 1); for (int i=begin; i<=end; i++) { ret.add(i); } return ret; }
... but it evades me. Using guava is fine.
Update:
Performance analysis
Since this question received some good answers, both using native Java 8 and third-party libraries, I decided to check the performance of all solutions.
The first test simply checks the creation of a list of 10 elements [1..10] using the following methods:
- classicArrayList : the above code in my question (and essentially the same as adarshr address).
- eclipseCollections : The code provided in Donald's answer below using Eclipse Collections 8.0.
- guavaRange : daveb code below . Technically, this does not create a
List<Integer> , but rather a ContiguousSet<Integer> , but since it implements the Iterable<Integer> in the order, it basically works for my purposes. - intStreamRange : the following Vladimir answer below, which uses
IntStream.rangeClosed() -, which was introduced in Java 8. - streamIterate : the code below , which also uses the
IntStream functionality introduced in Java 8.
Below are the results in kilo operations per second (higher numbers are better), for all of the above lists of size 10:

... and again for lists of size 10,000:

This last diagram is true - solutions other than Eclipse and Guava are too slow to even get one pixel bar! Quick solutions are 10,000 to 20,000 times faster than the rest.
What happens here, of course, is that the guava and eclipse solutions do not actually materialize a list of 10,000 elements - they are just fixed-size wrappers around the start and end points. Each element is created as needed during iteration. Since we are not actually iterating in this test, the value is delayed. All other solutions actually materialize the complete list in memory and pay a high price in the test just for creation.
Let me do something more realistic, as well as iterate over all integers, summing them up. Therefore, in the case of the IntStream.rangeClosed variant IntStream.rangeClosed benchmark is as follows:
@Benchmark public int intStreamRange() { List<Integer> ret = IntStream.rangeClosed(begin, end).boxed().collect(Collectors.toList()); int total = 0; for (int i : ret) { total += i; } return total; }
Here, the photographs change a lot, although solutions that do not require materialization are still the fastest. Here length = 10:

... and length = 10,000:

A long iteration over many elements significantly increases the value, but the eclipse and guava remain more than twice as fast even when testing for 10,000 elements.
So, if you really want a List<Integer> , the eclipse collection is probably the best choice, but, of course, if you use streams in a more native way (for example, forgetting .boxed() and making a reduction in a primitive domain), you are probably will be faster than all of these options.
1 Perhaps, with the exception of error handling, for example, if end < begin , or if the size exceeds some implementation limits or JVM (for example, arrays larger than 2^31-1 .