I came across a question related to ArrayList from java in a written test of a company. My request is just a small part of the actual question.
Suppose we have the following function to copy one ArrayList to another:
void function(List<E> l) { List<E> m = new ArrayList<E>(l); }
The question is mainly aimed at optimizing this copy operation. A list can contain a million entries. I tried the following approaches:
Collections.copy
System.arraycopy
addAll
But they all seem slower than this method. Do I need a method that is faster than this method, or is it the best method that is available?
java arraylist
Rohan bhalla
source share