3

I need a collection/array for primitive types (double).

I will add elements to a list/array only once during app startup. Elements will never be removed during program runtime. I know I'll never have to iterate over the whole array, only access elements by index. I also don't need concurrency.

What will be more memory friendly and have better performance - a primitive array or ArrayList/ArrayDeque/...?

3 Answers 3

4

If you know in advance the required size of your structure (and that size is fixed), a primitive array would give the best performance. Primitive data types exist for performance reasons, so using an array of double is faster than a Collection of Doubles (boxed doubles).

Other than the primitives vs. Objects issue, an ArrayList (which is backed by an array and supports random access) would have given a similar performance to an array, assuming you initialize it with the correct initial capacity, and it never has to be resized (if that's not the case, you can't use an array anyway).

Sign up to request clarification or add additional context in comments.

Comments

1

Using an array would be advantageous if you have fixed size and you want to get the elemnt by index.

Also it has advantage of getting contigous memory allocation, so later GC doesnt have to do hard work of compaction as would be the case in Collections (except ArrayList).

Comments

1

If are open to using a third party library, you might consider using an immutable primitive collection, even if you don't need concurrency. It will be almost as memory efficient and performant as a primitive array. You can build up the data using a mutable data structure at application start up, and then convert it to an immutable data structure once all the data is loaded. Eclipse Collections has a set of mutable and immutable primitive data structures that can be used. Here is an example showing up to build up a mutable list of 10K random doubles, and then converting the MutableDoubleList to an ImmutableDoubleList. This will have the effect of trimming the collection as well as making it immutable.

Random random = new Random();
MutableDoubleList list =
        random.doubles(10000, 0.0, 100.0)
                .collect(
                        DoubleLists.mutable::empty, 
                        MutableDoubleList::add, 
                        MutableDoubleList::addAll);

ImmutableDoubleList immutableDoubleList = list.toImmutable();
Assert.assertEquals(10000, immutableDoubleList.size());
System.out.println(immutableDoubleList.count(d -> d > 80.0d));
System.out.println(immutableDoubleList.get(0));
System.out.println(immutableDoubleList.get(99));
System.out.println(immutableDoubleList.get(999));
System.out.println(immutableDoubleList.get(9999));

I've seen this pattern used quite often with Eclipse Collections. The same pattern (mutable build at startup -> immutable) can be used with both object and primitive collections.

Note: I am a committer for Eclipse Collections.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.