Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of Arrays.asList

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?

like image 863
monksy Avatar asked Oct 12 '09 04:10

monksy


People also ask

What is the asList () method for the arrays class?

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 array asList () different?

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.

What is the difference between list of () and arrays asList ()?

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.

What is arrays asList () in Java?

asList returns a fixed-size list that is​ backed by the specified array; the returned list is serializable and allows random access.


1 Answers

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.)

like image 92
John Kugelman Avatar answered Sep 21 '22 22:09

John Kugelman