Java面试题|ArrayList和LinkedList的区别是什么?

Posted 千锋Java学院

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java面试题|ArrayList和LinkedList的区别是什么?相关的知识,希望对你有一定的参考价值。

每天一道面试模拟真题及解析





课前导读

●回复"每日一练"获取以前的题目,持续更新!

我希望大家积极参与!有什么不懂可以加小千微信进行讨论

★把面试准备工作,拆分、融入到平时每天


ArrayList和LinkedList的区别是什么?

参考答案:


  • ArrayList 基于动态数组实现的非线程安全的集合;LinkedList 基于双向链表实现的非线程安全的集合。
  • 扩容问题:ArrayList 使用数组实现,无参构造函数默认初始化长度为 10,数组扩容是会将原数组中的元素重新拷贝到新数组中,长度为原来的 1.5 倍(扩容代价高);LinkedList 不存在扩容问题,新增元素放到集合尾部,修改相应的指针节点即可。
  • LinkedList 比 ArrayList 更占内存,因为 LinkedList 为每一个节点存储了两个引用节点,一个指向前一个元素,一个指向下一个元素。
  • 对于随机 index 访问的 get 和 set 方法,一般 ArrayList 的速度要优于 LinkedList。因为 ArrayList 直接通过数组下标直接找到元素;LinkedList 要移动指针遍历每个元素直到找到为止。
  • 新增和删除元素,一般 LinkedList 的速度要优于 ArrayList。因为 ArrayList 在新增和删除元素时,可能扩容和复制数组;LinkedList 实例化对象需要时间外,只需要修改节点指针即可。
  • LinkedList 集合不支持高效的随机访问(RandomAccess)
  • ArrayList 的空间浪费主要体现在在list列表的结尾预留一定的容量空间;LinkedList 的空间花费则体现在它的每一个元素都需要消耗存储指针节点对象的空间。
都是非线程安全,允许存放 null
测试代码:
public static void main(String[] args) { ArrayList<Integer> arrayList = new ArrayList<Integer>(); LinkedList<Integer> linkedList = new LinkedList<Integer>(); int size = 10000 * 1000; int index = 5000 * 1000;
System.out.println("arrayList add " + size); addData(arrayList, size); System.out.println("linkedList add " + + size); addData(linkedList, size); System.out.println();
System.out.println("arrayList get " + index + " th"); getIndex(arrayList, index); System.out.println("linkedList get " + index + " th"); getIndex(linkedList, index); System.out.println();
System.out.println("arrayList set " + index + " th"); setIndex(arrayList, index); System.out.println("linkedList set " + index + " th"); setIndex(linkedList, index); System.out.println();
System.out.println("arrayList add " + index + " th"); addIndex(arrayList, index); System.out.println("linkedList add " + index + " th"); addIndex(linkedList, index); System.out.println();
System.out.println("arrayList remove " + index + " th"); removeIndex(arrayList, index); System.out.println("linkedList remove " + index + " th"); removeIndex(linkedList, index); System.out.println();
System.out.println("arrayList remove Object " + index); removeObject(arrayList, (Object)index); System.out.println("linkedList remove Object " + index); removeObject(linkedList, (Object)index); System.out.println();
System.out.println("arrayList add"); add(arrayList); System.out.println("linkedList add"); add(linkedList); System.out.println();
System.out.println("arrayList foreach"); foreach(arrayList); System.out.println("linkedList foreach"); foreach(linkedList); System.out.println();
System.out.println("arrayList forSize"); forSize(arrayList); System.out.println("linkedList forSize");// forSize(linkedList); System.out.println("cost time: ..."); System.out.println();
System.out.println("arrayList iterator"); ite(arrayList); System.out.println("linkedList iterator"); ite(linkedList); }
private static void addData(List<Integer> list, int size) { long s1 = System.currentTimeMillis(); for (int i = 0; i <size; i++) { list.add(i); } long s2 = System.currentTimeMillis(); System.out.println("cost time: " + (s2-s1)); }
private static void getIndex(List<Integer> list, int index) { long s1 = System.currentTimeMillis(); list.get(index); long s2 = System.currentTimeMillis(); System.out.println("cost time: " + (s2-s1)); }
private static void setIndex(List<Integer> list, int index) { long s1 = System.currentTimeMillis(); list.set(index, 1024); long s2 = System.currentTimeMillis(); System.out.println("cost time: " + (s2-s1)); }
private static void addIndex(List<Integer> list, int index) { long s1 = System.currentTimeMillis(); list.add(index, 1024); long s2 = System.currentTimeMillis(); System.out.println("cost time: " + (s2-s1)); }
private static void removeIndex(List<Integer> list, int index) { long s1 = System.currentTimeMillis(); list.remove(index); long s2 = System.currentTimeMillis(); System.out.println("cost time: " + (s2-s1)); }
private static void removeObject(List<Integer> list, Object obj) { long s1 = System.currentTimeMillis(); list.remove(obj); long s2 = System.currentTimeMillis(); System.out.println("cost time: " + (s2-s1)); }
private static void add(List<Integer> list) { long s1 = System.currentTimeMillis(); list.add(1024); long s2 = System.currentTimeMillis(); System.out.println("cost time: " + (s2-s1)); }
private static void foreach(List<Integer> list) { long s1 = System.currentTimeMillis(); for (Integer i : list) { //do nothing } long s2 = System.currentTimeMillis(); System.out.println("cost time: " + (s2-s1)); }
private static void forSize(List<Integer> list) { long s1 = System.currentTimeMillis(); int size = list.size(); for (int i = 0; i <size; i++) { list.get(i); } long s2 = System.currentTimeMillis(); System.out.println("cost time: " + (s2-s1)); }
private static void ite(List<Integer> list) { long s1 = System.currentTimeMillis(); Iterator<Integer> ite = list.iterator(); while (ite.hasNext()) { ite.next(); } long s2 = System.currentTimeMillis(); System.out.println("cost time: " + (s2-s1)); }

JDK1.8,win7 64位。结果
arrayList add 10000000cost time: 3309linkedList add 10000000cost time: 1375
arrayList get 5000000 thcost time: 0linkedList get 5000000 thcost time: 53
arrayList set 5000000 thcost time: 0linkedList set 5000000 thcost time: 44
arrayList add 5000000 thcost time: 3linkedList add 5000000 thcost time: 45
arrayList remove 5000000 thcost time: 3linkedList remove 5000000 thcost time: 46
arrayList remove Object 5000000cost time: 31linkedList remove Object 5000000cost time: 131
arrayList addcost time: 0linkedList addcost time: 0
arrayList foreachcost time: 30linkedList foreachcost time: 128
arrayList forSizecost time: 5linkedList forSizecost time: ...
arrayList iteratorcost time: 6linkedList iteratorcost time: 113

思考:

  • arrayList add 10000000 cost time: 3293;linkedList add 10000000 cost time: 1337

  • arrayList add 1000000  cost time: 22  ;   linkedList add 1000000   cost time: 1011

  • 跑另外一组数据,size 设为 1000 * 1000,得出当size增加,ArrayList 的 add操作的累计时间增长更快

  • 千万别在循环中调用 LinkedList 的 get 方法,耗时会让你崩溃

  • 代码例子中,"新增和删除元素,一般 LinkedList 的速度要优于 ArrayList" 并不成立,可以思考一下原因

点击 阅读原文 抢预约免费试听课程名额

以上是关于Java面试题|ArrayList和LinkedList的区别是什么?的主要内容,如果未能解决你的问题,请参考以下文章

Java面试题之Array和ArrayList的区别

Java面试题:ArrayList和LinkedList的区别

Java面试题|Array和ArrayList有何区别?

Java 面试题

Java面试宝典每日3题:day22

必问的Java集合框架面试题