Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toArray with pre sized array

when using ar.toArray(new String[ar.size()]) Android studio 3.2.1 warns about pre-sized array and recommends empty array:

There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]). In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation. This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).

is it true for Android or just for java?

using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).

because i think Android is non-HotSpot its virtual machine was Dalvik and now it is ART

like image 839
ygngy Avatar asked Nov 13 '18 15:11

ygngy


People also ask

What is the use of toArray () in Java?

The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.

What does public &lt T&gt T [] toArray T [] a mean?

public <T> T[] toArray(T[] a)The toArray() method is used to get an array which contains all the elements in ArrayList object in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein.

Can Java Initialize array with size 0?

Java allows creating an array of size zero. If the number of elements in a Java array is zero, the array is said to be empty. In this case you will not be able to store any element in the array; therefore the array will be empty.


2 Answers

Great question.

https://shipilev.net/blog/2016/arrays-wisdom-ancients/#_new_reflective_array

Bottom line: toArray(new T[0]) seems faster, safer, and contractually cleaner, and therefore should be the default choice now. Future VM optimizations may close this performance gap for toArray(new T[size]), rendering the current "believed to be optimal" usages on par with an actually optimal one. Further improvements in toArray APIs would follow the same logic as toArray(new T[0]) — the collection itself should create the appropriate storage.

like image 183
Waxhaw Avatar answered Sep 19 '22 10:09

Waxhaw


It reads since late updates of OpenJDK 6 and it does not matter which run-time is being used to run it - because the language-level of the code, which runs as compiled classes on Dalvik, might be Java 6, 7, 8. it only matters which language-level the project used to compile it. For example:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

Setting JavaVersion.VERSION_1_6 might possibly even disable the inspection complaint... fixing performance issues on these dated devices is probably not worth the effort - and some/most might not even be affected, because only the "earlier updates" behave different than all what followed up.

like image 45
Martin Zeitler Avatar answered Sep 23 '22 10:09

Martin Zeitler