Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I generate a list or array of sequential integers in Java?
    primarykey
    data
    text
    <p>Is there a short and sweet way to generate a <code>List&lt;Integer&gt;</code>, or perhaps an <code>Integer[]</code> or <code>int[]</code>, with sequential values from some <code>start</code> value to an <code>end</code> value?</p> <p>That is, something shorter than, but equivalent to<sup>1</sup> the following:</p> <pre><code>void List&lt;Integer&gt; makeSequence(int begin, int end) { List&lt;Integer&gt; ret = new ArrayList&lt;&gt;(end - begin + 1); for (int i=begin; i&lt;=end; i++) { ret.add(i); } return ret; } </code></pre> <p>The use of guava is fine.</p> <p><strong>Update:</strong></p> <h2>Performance Analysis</h2> <p>Since this question has received several good answers, both using native Java 8 and third party libraries, I thought I'd test the performance of all the solutions.</p> <p>The first test simply tests creating a list of 10 elements <code>[1..10]</code> using the following methods:</p> <ul> <li><strong>classicArrayList</strong>: the code given above in my question (and essentially the same as adarshr's answer).</li> <li><strong>eclipseCollections</strong>: the code given in <a href="https://stackoverflow.com/a/36350104/149138">Donald's answer</a> below using Eclipse Collections 8.0.</li> <li><strong>guavaRange</strong>: the code given in <a href="https://stackoverflow.com/a/10242491/149138">daveb's answer</a> below. Technically, this doesn't create a <code>List&lt;Integer&gt;</code> but rather a <code>ContiguousSet&lt;Integer&gt;</code> - but since it implements <code>Iterable&lt;Integer&gt;</code> in-order, it mostly works for my purposes.</li> <li><strong>intStreamRange</strong>: the code given in <a href="https://stackoverflow.com/a/22829036/149138">Vladimir's answer</a> below, which uses <code>IntStream.rangeClosed()</code> - which was introduced in Java 8.</li> <li><strong>streamIterate</strong>: the code given in <a href="https://stackoverflow.com/a/36114723/149138">Catalin's answer</a> below which also uses <code>IntStream</code> functionality introduced in Java 8.</li> </ul> <p>Here are the results in kilo-operations per second (higher numbers are better), for all the above with lists of size 10:</p> <p><a href="https://i.stack.imgur.com/j830l.png" rel="noreferrer"><img src="https://i.stack.imgur.com/j830l.png" alt="List creation throughput"></a></p> <p>... and again for lists of size 10,000:</p> <p><a href="https://i.stack.imgur.com/zdxOz.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zdxOz.png" alt="enter image description here"></a></p> <p>That last chart is correct - the solutions other than Eclipse and Guava are too slow to even get a single pixel bar! The fast solutions are 10,000 to 20,000 <em>times</em> faster than the rest.</p> <p>What's going on here, of course, is that the guava and eclipse solutions don't actually materialize any kind of 10,000 element list - they are simply fixed-size wrappers around the start and endpoints. Each element is created as needed during iteration. Since we don't actually iterate in this test, the cost is deferred. All of the other solutions actually materialize the full list in memory and pay a heavy price in a creation-only benchmark.</p> <p>Let's do something a bit more realistic and also iterate over all the integers, summing them. So in the case of the <code>IntStream.rangeClosed</code> variant, the benchmark looks like:</p> <pre><code>@Benchmark public int intStreamRange() { List&lt;Integer&gt; ret = IntStream.rangeClosed(begin, end).boxed().collect(Collectors.toList()); int total = 0; for (int i : ret) { total += i; } return total; } </code></pre> <p>Here, the pictures changes a lot, although the non-materializing solutions are still the fastest. Here's length=10:</p> <p><a href="https://i.stack.imgur.com/VQDYA.png" rel="noreferrer"><img src="https://i.stack.imgur.com/VQDYA.png" alt="List&lt;Integer&gt; Iteration (length=10)"></a> </p> <p>... and length = 10,000:</p> <p><a href="https://i.stack.imgur.com/LWWtu.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LWWtu.png" alt="List&lt;Integer&gt; Iteration (length=10,000)"></a></p> <p>The long iteration over many elements evens things up a lot, but eclipse and guava remain more than twice as fast even on the 10,000 element test.</p> <p>So if you <em>really</em> want a <code>List&lt;Integer&gt;</code>, eclipse collections seems like the best choice - but of course if you use streams in a more native way (e.g., forgetting <code>.boxed()</code> and doing a reduction in the primitive domain) you'll probably end up faster than all these variants. </p> <hr> <p><sup>1</sup> Perhaps with the exception of error handling, e.g., if <code>end</code> &lt; <code>begin</code>, or if the size exceeds some implementation or JVM limits (e.g., arrays larger than <code>2^31-1</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload