I am wondering what is the time complexity [in big O(n)
notations] of ArrayList
to Array
conversion:
ArrayList assetTradingList = new ArrayList();
assetTradingList.add("Stocks trading");
assetTradingList.add("futures and option trading");
assetTradingList.add("electronic trading");
assetTradingList.add("forex trading");
assetTradingList.add("gold trading");
assetTradingList.add("fixed income bond trading");
String [] assetTradingArray = new String[assetTradingList.size()];
assetTradingArray.toArray(assetTradingArray);
similarly, what is the time complexity for arrays to list in the following ways:
method 1 using Arrays.asList
:
String[] asset = {"equity", "stocks", "gold", "foreign exchange","fixed
income", "futures", "options"};
List assetList = Arrays.asList(asset);
method 2 using collections.addAll
:
List assetList = new ArrayList();
String[] asset = {"equity", "stocks", "gold", "foreign exchange", "fixed
income", "futures", "options"};
Collections.addAll(assetList, asset);
method 3 addAll
:
ArrayList newAssetList = new ArrayList();
newAssetList.addAll(Arrays.asList(asset));
The reason I am interested in the overhead of copying back and forth is because in typical interviews, questions come such as given an array of pre-order traversal elements, convert to binary search tree
and so on, involving arrays
. With List
offering a whole bunch of operations such as remove
etc, it would make it simple to code using List
than Array
.
In which case, I would like to defend me for using list
instead of arrays
saying "I would first convert the Array to List because the overhead of this operation is not much (hopefully)".
Any better methods recommended for copying the elements back and forth from array to list
that would be faster would be good know too.
Thanks
The ArrayList in Java is backed by an array. So let's focus first on the time complexity of the common operations at a high level: add() – takes O(1) time; however, worst-case scenario, when a new array has to be created and all the elements copied to it, it's O(n) add(index, element) – on average runs in O(n) time.
2^log(n)-1 time (assuming doubling for easier formatting).
The time complexity for resizing is O(n) . It will create a new array with double the capacity and copy over each element from the original array into the new array. This requires a full iteration. Note that this can be optimized internally by the JVM though.
ArrayList has O(1) time complexity to access elements via the get and set methods. LinkedList has O(n/2) time complexity to access the elements.
It would seem that Arrays.asList(T[]);
is the fastest withO(1)
Because the method returns an unmodifiable List
, there is no reason to copy the references over to a new data structure. The method simply uses the given array as a backing array for the unmodifiable List
implementation that it returns.
The other methods seem like they copy each element, one by one to an underlying data structure. ArrayList#toArray(..)
uses System.arraycopy(..)
deep down (O(n)
but faster because it's done natively). Collections.addAll(..)
loops through the array elements (O(n)
).
Careful when using ArrayList
. The backing array doubles in size when its capacity is reached, ie. when it's full. This takes O(n)
time. Adding to an ArrayList
might not be the best idea unless you know how many elements you are adding from the beginning and create it with that size.
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