5

In java, basically these two conversions are so common and popular...

  • Array to List

    List<String> list = Arrays.asList(array);

  • List to Array

    String[] array = list.toArray(new String[list.size()]);

So the question is, which is faster, Array to List or its reverse?

Now, Why am I asking this question? Because I've to implement a method in which an array or a list, both can be passed in parameter. I just have to iterate this list and that's all. So here I have to decide that what should I convert? array to list or list to array!

14
  • 2
    That's not type casting. Commented Mar 7, 2018 at 9:37
  • 3
    Why don't you run a benchmark on your own? Commented Mar 7, 2018 at 9:37
  • 5
    It's conversion, and it doesn't matter one bit which is faster. You should use whichever fits the design better. You can even implement both. Commented Mar 7, 2018 at 9:38
  • 2
    Beware of premature optimization Never choose a certain syntax or style for performance consideration unless you have proven by measurement that you actually have a performance problem and the code in question is really the bottleneck and the alternative really solves the problem. Commented Mar 7, 2018 at 9:38
  • 2
    It really doesn't matter which one you choose. It certainly doesn't matter enough to waste time on. Commented Mar 7, 2018 at 9:41

3 Answers 3

17

Arrays.asList is faster because it does not copy data - it returns an object wrapping the array passed to it, and implementing the List interface. Collection.toArray() copies the data to the array, so it runs in O(N) instead of O(1).

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

5 Comments

Added note: This does not necessarily mean the OP's code will be faster. OP mentions that they already accept two overloads, an Array and a List. OP would need to gather data on how often each overload is called to determine if transforming to a List would be faster. If %99.99 use the Array overload you might want to stick with the array implementation
Are you really sure that doing intensive random access on a LinkedList will be better than converting it first to an array?
yole answered the question exactly. Arrays.asList is the (theoretically) faster method. Whether it is the right way to go for a special use-case is a completely other question.
ArrayList copies data using System.arraycopy. I don't think that it would be observably slower.
For the majority of questions asked on SO about performance, there won't be any observable difference between the options considered by OP. However, when there's an essential difference in behavior which has a performance impact, it's still useful to highlight and understand it.
1

Arrays.asList will faster since it doesnt have to copy any data instead it has to just pack it.

2 Comments

Pack? There is no "packing" going on here. Perhaps you mean "wrap"?
yup thats true!!
0

IMHO, you are handling the rod the wrong side. The good question to ask is whether you need an array or List in your method. By the way, List it just an interface, and many implementations could exist. For example the JDK directly provides ArrayList which is backed by an array, so conversion to array should be fast, and LinkedList which is a doubly linked implementation, and many others including abstract classes intended to help for custom implementations.

In short, you know how you want to use the elements in your method, so just use the more appropriate container.

In my opininion, if you intend to iterate the elements, use a List, if you need intensive random access and could receive an linked list implementation, then use an Array.

2 Comments

ArrayList.toArray() still performs a copy, so it still runs in O(N), even though ArrayList is indeed backed by an array.
@yole You are right. I just meant that the conversion time was not necessarily the more important. Maybe I have tried to answer the question OP should have asked and not the one he actually asked ;-)

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.