ArrayList和LinkedList的效率到底哪个高?

今天面试遇到一个问题,ArrayList和LinkedList的区别?

这个问题很常见也很容易就能答上来了,没想到我说完ArrayList使用尾插法插入大批量数据的时候比LinkedList插入效率更高之后,面试官又问了一下当不知道元素的下标的时候ArrayList和LinkedList的效率到底哪个高?这直接给我整不会了。

后面查了一下资料,自己写代码实际操作了一下发现还是分几种情况的,下面是测试时候的代码

 

 1 public class Test
 2 {
 3 
 4     public static void main(String[] args) {
 5         int count = 50000;
 6 
 7         System.out.println("通过for循环遍历ArrayList:" + getByFor(new ArrayList<>(count), count) + "ms");
 8         System.out.println("通过for循环遍历LinkedList:" + getByFor(new LinkedList<>(), count) + "ms");
 9 
10         System.out.println("通过foreach遍历ArrayList:" + getByForeach(new ArrayList<>(count), count) + "ms");
11         System.out.println("通过foreach遍历LinkedList:" + getByForeach(new LinkedList<>(), count) + "ms");
12 
13         System.out.println("通过Iterator遍历ArrayList:" + getByForIterator(new ArrayList<>(count), count) + "ms");
14         System.out.println("通过Iterator遍历LinkedList:" + getByForIterator(new LinkedList<>(), count) + "ms");
15     }
16 
17     /**
18      * 通过for循环遍历List
19      *
20      * @param list  list
21      * @param count 遍历元素的个数
22      * @return 所耗费的时间(单位:ms)
23      */
24     public static double getByFor(List<String> list, int count) {
25         for (int i = 0; i < count; i++) {
26             list.add("onemore-" + i);
27         }
28         String name = "万猫学社";
29         long start = System.nanoTime();
30         for (int i = 0; i < count; i++) {
31             if (name.equals(list.get(i))) {
32                 System.out.println(name);
33             }
34         }
35         long end = System.nanoTime();
36         return (end - start) / 1000000.0;
37     }
38 
39     /**
40      * 通过foreach遍历List
41      *
42      * @param list  list
43      * @param count 遍历元素的个数
44      * @return 所耗费的时间(单位:ms)
45      */
46     public static double getByForeach(List<String> list, int count) {
47         for (int i = 0; i < count; i++) {
48             list.add("onemore-" + i);
49         }
50         String name = "万猫学社";
51         long start = System.nanoTime();
52         for (String str : list) {
53             if (name.equals(str)) {
54                 System.out.println(name);
55             }
56         }
57         long end = System.nanoTime();
58         return (end - start) / 1000000.0;
59     }
60     /**
61      * 通过Iterator迭代器遍历List
62      *
63      * @param list  list
64      * @param count 遍历元素的个数
65      * @return 所耗费的时间(单位:ms)
66      */
67     public static double getByForIterator(List<String> list, int count) {
68         for (int i = 0; i < count; i++) {
69             list.add("onemore-" + i);
70         }
71         String name = "万猫学社";
72         Iterator<String> iterator = list.iterator();
73         long start = System.nanoTime();
74         while (iterator.hasNext()) {
75             if (name.equals(iterator.next())) {
76                 System.out.println(name);
77             }
78         }
79         long end = System.nanoTime();
80         return (end - start) / 1000000.0;
81     }
82 
83 }

下面是运行结果:

 

 

 然后发现ArrayList只有在for循环的时候效率是比LinkedList高很多,在使用迭代器的时候反而比LinkedList效率低,用foreach的时候比linkedList差一点点相差不大

代码参考资料:https://blog.csdn.net/heihaozi/article/details/123773564

posted @ 2022-06-09 23:07  李晓书  阅读(154)  评论(0编辑  收藏  举报