Array or List in Java. Which is faster?

后端 未结 30 1672
滥情空心
滥情空心 2020-11-22 04:30

I have to keep thousands of strings in memory to be accessed serially in Java. Should I store them in an array or should I use some kind of List ?

Since arrays keep

30条回答
  •  梦毁少年i
    2020-11-22 05:05

    Since there are already a lot of good answers here, I would like to give you some other information of practical view, which is insertion and iteration performance comparison : primitive array vs Linked-list in Java.

    This is actual simple performance check.
    So, the result will depend on the machine performance.

    Source code used for this is below :

    import java.util.Iterator;
    import java.util.LinkedList;
    
    public class Array_vs_LinkedList {
    
        private final static int MAX_SIZE = 40000000;
    
        public static void main(String[] args) {
    
            LinkedList lList = new LinkedList(); 
    
            /* insertion performance check */
    
            long startTime = System.currentTimeMillis();
    
            for (int i=0; i

    Performance Result is below :

提交回复
热议问题