If your starting point is a LinkedList<Integer>
, just replacing it with ArrayList<Integer>
will reduce the memory consumption significantly, as explained in When to use LinkedList over ArrayList?. Since boxed integers are small objects and some of them will even be reused when boxing the same value, the graphs of this answer have significance.
If you want to have it simpler, just use int[]
. If you need something you can fill incrementally, the Stream API has indeed an option. Use IntStream.builder()
to get an IntStream.Builder
to which you can repeatedly add new int
values. Once it contains all values, you can call build().toArray()
on it to get an int[]
array containing all values or you can directly perform filtering and aggregate operations on the IntStream
returned by build()
(if you can express the aggregate operation as reduction).