正确使用ArrayList和LinkedList—性能的改进

原创 2003年07月03日 00:26:00

USING ARRAYLIST AND LINKEDLIST

ArrayList and LinkedList are two Collections classes used for storing lists of object references. For example, you could have an ArrayList of Strings, or a LinkedList of Integers. This tip compares the performance of ArrayList and LinkedList, and offers some suggestions about which of these classes is the right choice in a given situation.

The first key point is that an ArrayList is backed by a primitive Object array. Because of that, an ArrayList is much faster than a LinkedList for random access, that is, when accessing arbitrary list elements using the get method. Note that the get method is implemented for LinkedLists, but it requires a sequential scan from the front or back of the list. This scan is very slow. For a LinkedList, there's no fast way to access the Nth element of the list.

Consider the following example. Suppose you have a large list of sorted elements, either an ArrayList or a LinkedList. Suppose too that you do a binary search on the list. The standard binary search algorithm starts by checking the search key against the value in the middle of the list. If the middle value is too high, then the upper half of the list is eliminated. However, if the middle value is too low, then the lower half of the list is ignored. This process continues until the key is found in the list, or until the lower bound of the search becomes greater than the upper bound.

Here's a program that does a binary search on all the elements in an ArrayList or a LinkedList:

import java.util.*;

public class ListDemo1 {
static final int N = 10000;

static List values;

// make List of increasing Integer values

static {
Integer vals[] = new Integer[N];

Random rn = new Random();

for (int i = 0, currval = 0; i < N; i++) {
vals[i] = new Integer(currval);
currval += rn.nextInt(100) + 1;
}

values = Arrays.asList(vals);
}

// iterate across a list and look up every
// value in the list using binary search

static long timeList(List lst) {
long start = System.currentTimeMillis();

for (int i = 0; i < N; i++) {

// look up a value in the list
// using binary search

int indx = Collections.binarySearch(
lst, values.get(i));

// sanity check for result
// of binary search

if (indx != i) {
System.out.println(
"*** error ***/n");
}
}

return System.currentTimeMillis() - start;
}

public static void main(String args[]) {

// do lookups in an ArrayList

System.out.println("time for ArrayList = " +
timeList(new ArrayList(values)));

// do lookups in a LinkedList

System.out.println(
"time for LinkedList = " +
timeList(new LinkedList(values)));
}
}

The ListDemo1 program sets up a List of sorted Integer values. It then adds the values to an ArrayList or a LinkedList. Then Collections.binarySearch is used to search for each value in the list.

When you run this program, you should see a result that looks something like this:

time for ArrayList = 31

time for LinkedList = 4640

ArrayList is about 150 times faster than LinkedList. (Your results might differ depending on your machine characteristics, but you should see a distinct difference in the result for ArrayList as compared to that for LinkedList. The same is true for the other programs in this tip.) Clearly, LinkedList is a bad choice in this situation. The binary search algorithm inherently uses random access, and LinkedList does not support fast random access. The time to do a random access in a LinkedList is proportional to the size of the list. By comparison, random access in an ArrayList has a fixed time.

You can use the RandomAccess marker interface to check whether a List supports fast random access:

void f(List lst) {
if (lst instanceof RandomAccess) {
// supports fast random access
}
}

ArrayList implements the RandomAccess interface, and LinkedList. does not. Note that Collections.binarySearch does take advantage of the RandomAccess property, to optimize searches.

Do these results prove that ArrayList is always a better choice? Not necessarily. There are many cases where LinkedList does better. Also note that there are many situations where an algorithm can be implemented efficiently for LinkedList. An example is reversing a LinkedList using Collections.reverse. The internal algorithm does this, and gets reasonable performance, by using forward and backward iterators.

Let's look at another example. Suppose you have a list of elements, and you do a lot of element inserting and deleting to the list. In this case, LinkedList is the better choice. To demonstrate that, consider the following "worst case" scenario. In this demo, a program repeatedly inserts elements at the beginning of a list. The code looks like this:

import java.util.*;

public class ListDemo2 {
static final int N = 50000;

// time how long it takes to add
// N objects to a list

static long timeList(List lst) {
long start = System.currentTimeMillis();

Object obj = new Object();

for (int i = 0; i < N; i++) {
lst.add(0, obj);
}

return System.currentTimeMillis() - start;
}

public static void main(String args[]) {

// do timing for ArrayList

System.out.println(
"time for ArrayList = " +
timeList(new ArrayList()));

// do timing for LinkedList

System.out.println(
"time for LinkedList = " +
timeList(new LinkedList()));
}
}

When you run this program, the result should look something like this:

time for ArrayList = 4859

time for LinkedList = 125

These results are pretty much the reverse of the previous example.

When an element is added to the beginning of an ArrayList, all of the existing elements must be pushed back, which means a lot of expensive data movement and copying. By contrast, adding an element to the beginning of a LinkedList simply means allocating an internal record for the element and then adjusting a couple of links. Adding to the beginning of a LinkedList has fixed cost, but adding to the beginning of an ArrayList has a cost that's proportional to the list size.

So far, this tip has looked at speed issues, but what about space? Let's look at some internal details of how ArrayList and LinkedList are implemented in Java 2 SDK, Standard Edition v 1.4. These details are not part of the external specification of these classes, but are illustrative of how such classes work internally.

The LinkedList class has a private internal class defined like this:

private static class Entry {
Object element;
Entry next;
Entry previous;
}

Each Entry object references a list element, along with the next and previous elements in the LinkedList -- in other words, a doubly-linked list. A LinkedList of 1000 elements will have 1000 Entry objects linked together, referencing the actual list elements. There is significant space overhead in a LinkedList structure, given all these Entry objects.

An ArrayList has a backing Object array to store the elements. This array starts with a capacity of 10. When the array needs to grow, the new capacity is computed as:

newCapacity = (oldCapacity * 3) / 2 + 1;

Notice that the array capacity grows each time by about 50%. This means that if you have an ArrayList with a large number of elements, there will be a significant amount of space wasted at the end. This waste is intrinsic to the way ArrayList works. If there was no spare capacity, the array would have to be reallocated for each new element, and performance would suffer dramatically. Changing the growth strategy to be more aggressive (such as doubling the size at each reallocation) would result in slightly better performance, but it would waste more space.

If you know how many elements will be in an ArrayList, you can specify the capacity to the constructor. You can also call the trimToSize method after the fact to reallocate the internal array. This gets rid of the wasted space.

So far, this discussion has assumed that either an ArrayList or a LinkedList is "right" for a given application. But sometimes, other choices make more sense. For example, consider the very common situation where you have a list of key/value pairs, and you would like to retrieve a value for a given key.

You could store the pairs in an N x 2 Object array. To find the right pair, you could do a sequential search on the key values. This approach works, and is a useful choice for very small lists (say 10 elements or less), but it doesn't scale to big lists.

Another approach is to sort the key/value pairs by ascending key value, store the result in a pair of ArrayLists, and then do a binary search on the keys list. This approach also works, and is very fast. Yet another approach is to not use a list structure at all, but instead use a map structure (hash table), in the form of a HashMap.

Which is faster, a binary search on an ArrayList, or a HashMap? Here's a final example that compares these two:

import java.util.*;

public class ListDemo3 {
static final int N = 500000;

// Lists of keys and values

static List keys;
static List values;

// fill the keys list with ascending order key
// values and fill the values list with
// corresponding values (-key)

static {
Integer keyvec[] = new Integer[N];
Integer valuevec[] = new Integer[N];

Random rn = new Random();

for (int i = 0, currval = 0; i < N; i++) {
keyvec[i] = new Integer(currval);
valuevec[i] = new Integer(-currval);
currval += rn.nextInt(100) + 1;
}

keys = Arrays.asList(keyvec);
values = Arrays.asList(valuevec);
}

// fill a Map with key/value pairs

static Map map = new HashMap();

static {
for (int i = 0; i < N; i++) {
map.put(keys.get(i), values.get(i));
}
}

// do binary search lookup of all keys

static long timeList() {
long start = System.currentTimeMillis();

for (int i = 0; i < N; i++) {
int indx = Collections.binarySearch(
keys, keys.get(i));

// sanity check of returned value
// from binary search

if (indx != i) {
System.out.println(
"*** error ***/n");
}
}

return System.currentTimeMillis() - start;
}

// do Map lookup of all keys

static long timeMap() {
long start = System.currentTimeMillis();

for (int i = 0; i < N; i++) {
Integer value = (Integer)map.get(
keys.get(i));

// sanity check of value returned
// from map lookup

if (value != values.get(i)) {
System.out.println(
"*** error ***/n");
}
}

return System.currentTimeMillis() - start;
}

public static void main(String args[]) {

// do timing for List implementation

System.out.println("List time = " +
timeList());

// do timing for Map implementation

System.out.println("Map time = " +
timeMap());
}
}

The program sets up Lists of keys and values, and then uses two different techniques to map keys to values. One approach uses a binary search on a list, the other a hash table.

When you run the ListDemo3 program, you should get a result that looks something like this:

ArrayList time = 1000

HashMap time = 281

In this example, N has a value of 500000. Approximately, log2(N) - 1 comparisons are required in an average successful binary search, so each binary search lookup in the ArrayList will take about 18 comparisons. By contrast, a properly implemented hash table typically requires only 1-3 comparisons. So you should expect the hash table to be faster in this case.

However, binary search is still useful. For example, you might want to do a lookup in a sorted list and then find keys that are close in value to the key used for the lookup. Doing this is easy with binary search, but impossible in a hash table. Keys in a hash table are stored in apparent random order. Also, if you are concerned with worst-case performance, the binary search algorithm offers a much stronger performance guarantee than a hash table scheme. You might also consider using TreeMap for doing lookups in sorted collections of key/value pairs.

Let's summarize the key points presented in this tip:

Appending elements to the end of a list has a fixed averaged cost for both ArrayList and LinkedList. For ArrayList, appending typically involves setting an internal array location to the element reference, but occasionally results in the array being reallocated. For LinkedList, the cost is uniform and involves allocating an internal Entry object.
Inserting or deleting elements in the middle of an ArrayList implies that the rest of the list must be moved. Inserting or deleting elements in the middle of a LinkedList has fixed cost.
A LinkedList does not support efficient random access
An ArrayList has space overhead in the form of reserve capacity at the end of the list. A LinkedList has significant space overhead per element.
Sometimes a Map structure is a better choice than a List.

说出 ArrayList,Vector, LinkedList 的存储性能和特性?

ArrayList 和 Vector 都是使用数组方式存储数据,此数组元素数大于实际 存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元 素要涉及数组元素移动等内存操作,所以索...
  • uniquewonderq
  • uniquewonderq
  • 2015年06月09日 14:38
  • 2294

一道关于:ArrayList、Vector、LinkedList的存储性能和特性 的面试题

ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素,但是插入数据要设计到数组元素移动等内存操作,所以索引数据快插入数据慢...
  • u014044812
  • u014044812
  • 2015年09月09日 22:46
  • 2187

Java ArrayList、LinkedList和Vector的使用及性能分析

第1部分 List概括 List 是一个接口,它继承于Collection的接口。它代表着有序的队列。 AbstractList 是一个抽象类,它继承于AbstractCollection。A...
  • guoweimelon
  • guoweimelon
  • 2016年03月04日 11:01
  • 1716

ArrayList和LinkedList优缺点比较

一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。 2.对于随机访问get和set,...
  • hdblocal
  • hdblocal
  • 2016年08月01日 15:25
  • 1008

Java中arraylist和linkedlist源码分析与性能比较

Java中arraylist和linkedlist源码分析与性能比较 1,简介 在java开发中比较常用的数据结构是arraylist和linkedlist,本文主要从源码角度分析arraylis...
  • weibin_6388
  • weibin_6388
  • 2016年04月05日 14:17
  • 607

ArrayList,Vector,LinkedList的存储性能和特性

 ArrayList和Vector都是使用数组方式存储数据,此 数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索...
  • u014742281
  • u014742281
  • 2015年06月01日 14:57
  • 1116

测试ArrayList和LinkedList的效率

测试代码如下 import java.util.ArrayList; import java.util.LinkedList; public class arraylistPKlinked...
  • sun11462
  • sun11462
  • 2015年07月29日 16:44
  • 793

ArrayList和LinkedList的几种循环遍历方式及性能对比分析 (十)

ArrayList和LinkedList的几种循环遍历方式及性能对比分析 (十)
  • u014608640
  • u014608640
  • 2016年09月12日 15:09
  • 863

ArrayList和LinkedList add和remove方法的比较

ArrayList 是一种可增长的数组的实现。 使用ArrayList的优点在于 对 get和set的调用是花费常数时间。缺点就是有新的项插入,和现有的项删除代价昂贵,除非变动是 在ArrayList...
  • u012859591
  • u012859591
  • 2017年04月13日 12:46
  • 618

小谈面试时面试官为什么问ArrayList,LinkedList与List的不同

当你做为一个技术面试官面试应聘者时,你总是想尝试能全面了解这个面试者的方方面面。技术,背景,性格都是被重点关注的。面试官想在短暂的面试过程中多了解应聘者,那就要有明确的目的性。但是一般面试官不会只是简...
  • yuyanjun123
  • yuyanjun123
  • 2016年06月22日 16:29
  • 434
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:正确使用ArrayList和LinkedList—性能的改进
举报原因:
原因补充:

(最多只允许输入30个字)