原文: https://javatutorial.net/difference-between-arraylist-and-linkedlist-in-java

本文介绍了ArrayListLinkedList之间的区别,在这种情况下,我们应该优先选择一个。

由于继承了相同的接口 – List,因此ArrayListLinkedList共享相同的属性。 但是ArrayListLinkedList有什么区别? 简单地说 – ArrayList对于一次写入多次读取操作很有用,但不利于从前端或中间进行添加/删除。 另一方面,LinkedList更适合插入和删除数据。

性能表现

下表通过对LinkedListArrayList执行不同的操作来显示平均算法复杂度

Java 中`ArrayList`和`LinkedList`之间的区别 - 图1

ArrayListLinkedList操作的复杂度

ArrayListLinkedList性能示例

下面的示例演示使用ArrayListLinkedList上的相同数据进行addsetremove操作的性能

  1. package javatutorial.net;
  2. import java.util.ArrayList;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. public class ArrayListVsLinkedListExample {
  6. private static final int ELCOUNT = 50000;
  7. public static void main(String[] args) {
  8. List<String> alist = new ArrayList<String>();
  9. List<String> llist = new LinkedList<String>();
  10. // Insertion ////////////////
  11. // ArrayList
  12. long start = System.currentTimeMillis();
  13. for (int i = 0; i < ELCOUNT; i++) {
  14. alist.add("element #" + i);
  15. }
  16. long totalTimeMs = System.currentTimeMillis() - start;
  17. System.out.println("Adding 50K elements in ArrayList took " + totalTimeMs + " ms");
  18. // LinkedList
  19. start = System.currentTimeMillis();
  20. for (int i = 0; i < ELCOUNT; i++) {
  21. llist.add("element #" + i);
  22. }
  23. totalTimeMs = System.currentTimeMillis() - start;
  24. System.out.println("Adding 50K elements in LinkedList took " + totalTimeMs + " ms");
  25. // Modification /////////////
  26. // ArrayList
  27. start = System.currentTimeMillis();
  28. for (int i = 0; i < ELCOUNT; i++) {
  29. alist.set(i, "modified element #" + i);
  30. }
  31. totalTimeMs = System.currentTimeMillis() - start;
  32. System.out.println("Modifying 50K elements in ArrayList took " + totalTimeMs + " ms");
  33. // LinkedList
  34. start = System.currentTimeMillis();
  35. for (int i = 0; i < ELCOUNT; i++) {
  36. llist.set(i, "modified element #" + i);
  37. }
  38. totalTimeMs = System.currentTimeMillis() - start;
  39. System.out.println("Modifying 50K elements in LinkedList took " + totalTimeMs + " ms");
  40. // Removal //////////////////
  41. System.out.println("ArrayList size before removal " + alist.size());
  42. System.out.println("LinkedList size before removal " + llist.size());
  43. // ArrayList
  44. start = System.currentTimeMillis();
  45. for (int i = 0; i < ELCOUNT; i++) {
  46. alist.remove(0);
  47. }
  48. totalTimeMs = System.currentTimeMillis() - start;
  49. System.out.println("Removing 50K elements in ArrayList took " + totalTimeMs + " ms");
  50. // LinkedList
  51. start = System.currentTimeMillis();
  52. for (int i = 0; i < ELCOUNT; i++) {
  53. llist.remove(0);
  54. }
  55. totalTimeMs = System.currentTimeMillis() - start;
  56. System.out.println("Removing 50K elements in LinkedList took " + totalTimeMs + " ms");
  57. System.out.println("ArrayList size after removal " + alist.size());
  58. System.out.println("LinkedList size after removal " + llist.size());
  59. }
  60. }

这是执行示例代码的输出。 结果将因不同的计算机配置而异

  1. Adding 50K elements in ArrayList took 10 ms
  2. Adding 50K elements in LinkedList took 7 ms
  3. Modifying 50K elements in ArrayList took 7 ms
  4. Modifying 50K elements in LinkedList took 6315 ms
  5. ArrayList size before removal 50000
  6. LinkedList size before removal 50000
  7. Removing 50K elements in ArrayList took 135 ms
  8. Removing 50K elements in LinkedList took 4 ms
  9. ArrayList size after removal 0
  10. LinkedList size after removal 0

如您在上面的输出中看到的:

  • LinkedList在访问和修改元素方面明显较慢
  • LinkedList添加元素的速度较慢
  • LinkedList从列表开头删除元素的速度要快得多
  • ArrayList插入新元素的速度较慢
  • ArrayList在访问和修改元素方面明显更快
  • ArrayList从列表开头删除元素的速度明显较慢

结论

在所有情况下,算法复杂度和运算性能都不是恒定的。 您必须考虑两个主要因素 - 列表的大小以及我们使用的元素在列表中的放置位置(在开头,中间或结尾)。 唯一不变的规则是:如果要更快地检索元素,请使用ArrayList,如果要更快地处理数据,请使用LinkedList