What is the cost of using Arrays.asList to convert static object arrays? Assuming that the object array has N items is it just an O(N) algorithm, where all of the items are copied by reference or is it merely a facade where the original array is put behind a List facade?
The asList() method of java. util. Arrays class is used to return a fixed-size list backed by the specified array. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.
How is Arrays. asList() different than the standard way of initialising List? Explanation: List returned by Arrays. asList() is a fixed length list which doesn't allow us to add or remove element from it.
Only this list is a view of the original list, so it can change if you change the original list. Arrays. asList is not completely immutable, it does not have a restriction on set . Similarly, changing the backing array (if you hold it) will change the list.
asList returns a fixed-size list that is backed by the specified array; the returned list is serializable and allows random access.
It is cheap, O(1). As you suspect the list is merely a wrapper around the array. This is confirmed by the Java API documentation:
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With