Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Apart from using a database, if you just have a number of intergers you could write them to a plain file. Plain files retain order, however removing entries can be expensive.</p> <p>You can write/rewrite 1 million integers in about 0.1 seconds using a plain file.</p> <p>An efficient collecton for int primitives is TIntArrayList. Its like @JPelletier's suggestion but wraps a int[]. A million int values should take about 4 MB of memory or disk.</p> <p>EDIT: This shows that for 1 million numbers ArrayList is a bad choice. Mainly because remove(0) is O(n) rather than O(1)</p> <pre><code>import java.util.ArrayList; import java.util.LinkedList; import java.util.List; // based on http://www.cs.princeton.edu/introcs/43stack/RingBuffer.java.html public class IntRingBuffer { private final int[] a; // queue elements private int N = 0; // number of elements on queue private int first = 0; // index of first element of queue private int last = 0; // index of next available slot // cast needed since no generic array creation in Java public IntRingBuffer(int capacity) { a = new int[capacity]; } public boolean isEmpty() { return N == 0; } public int size() { return N; } public void enqueue(int item) { if (N == a.length) { throw new RuntimeException("Ring buffer overflow"); } a[last] = item; last = (last + 1) % a.length; // wrap-around N++; } // remove the least recently added item - doesn't check for underflow public int dequeue() { if (isEmpty()) { throw new RuntimeException("Ring buffer underflow"); } int item = a[first]; N--; first = (first + 1) % a.length; // wrap-around return item; } public static void main(String... args) { int size = 1000000; { long start = System.nanoTime(); IntRingBuffer list = new IntRingBuffer(size); for(int i=0;i&lt; size;i++) list.enqueue(i); for(int i=0;i&lt; size;i++) list.dequeue(); long time = System.nanoTime() - start; System.out.println(list.getClass().getSimpleName()+": Took "+time/1000/1000+" ms to add/remove "+size+" elements"); } { long start = System.nanoTime(); List&lt;Integer&gt; list = new LinkedList&lt;Integer&gt;(); for(int i=0;i&lt; size;i++) list.add(i); for(int i=0;i&lt; size;i++) list.remove(0); long time = System.nanoTime() - start; System.out.println(list.getClass().getSimpleName()+": Took "+time/1000/1000+" ms to add/remove "+size+" elements"); } { long start = System.nanoTime(); List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(); for(int i=0;i&lt; size;i++) list.add(i); for(int i=0;i&lt; size;i++) list.remove(0); long time = System.nanoTime() - start; System.out.println(list.getClass().getSimpleName()+": Took "+time/1000/1000+" ms to add/remove "+size+" elements"); } } } </code></pre> <p>Prints</p> <pre><code>IntRingBuffer: Took 31 ms to add/remove 1000000 elements LinkedList: Took 252 ms to add/remove 1000000 elements ArrayList: Took 325832 ms to add/remove 1000000 elements </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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