Summary
- Together with the vector (thread-safe) list all subclasses
- ArrayList LinkedList doubly linked list data structure based on the structure of the array-based data
- ArrayList returned directly query data, LinkedList need to traverse the list, so the random queries more efficient ArrayList
- ArrayList required when additions or deletions of position of the mobile element, the array needs to copy, the LinkedList need to position the elements found in the inserted position, and then to add a pointer, when compared with the amount of data almost hr (10,000 or less) performance, but the amount of data representing when large LinkedList more efficient
1. subclass with the vector (thread-safe) list of all
2.ArrayList LinkedList array-based data structure data structure based on two-way linked list
// ArrayList
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
//LinkedList
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;
if (index == size) {
succ = null;
pred = last;
} else {
succ = node(index);
pred = succ.prev;
}
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)
first = newNode;
else
pred.next = newNode;
pred = newNode;
}
if (succ == null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
复制代码
3.ArrayList direct return query data, LinkedList need to traverse the list, so the random queries more efficient ArrayList
//ArrayList
public E get(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
return (E) elementData[index];
}
//LinkedList
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
复制代码
4.ArrayList required location of the mobile element when deletions, copy arrays required, the LinkedList need to position the elements found in the insertion position, and then to add a pointer, when compared with the amount of data almost hr (10,000 or less) performance, but the data when a large amount of relatively high efficiency LinkedList
//ArrayList
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
//LinkedList
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
final Node<E> pred = succ.prev;
final Node<E> newNode = new Node<>(pred, e, succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
复制代码
5. Test Program
public class Test1 {
static List<Integer> arrayData =new ArrayList<Integer>();
static List<Integer> linkedData =new LinkedList<Integer>();
public static int maxCount = 30000;
public static void main(String[] args) {
for(int i=0;i<maxCount;i++){
arrayData.add(i);
linkedData.add(i);
}
System.out.println("maxCount:"+maxCount);
//获得两者随机访问的时间
System.out.println("arrayData get time:"+getTime(arrayData));
System.out.println("linkedData get time:"+getTime(linkedData));
//获得两者插入数据的时间
System.out.println("arrayData insert time:"+insertTime(arrayData));
System.out.println("linkedData insert time:"+insertTime(linkedData));
}
//获取数据
public static long getTime(List<Integer> list){
long time=System.currentTimeMillis();
for(int i = 0; i < maxCount; i++){
int getData = list.get(i);
}
return System.currentTimeMillis()-time;
}
//插入数据
public static long insertTime(List<Integer> list){
long num = maxCount;
int index = 1000;
long time=System.currentTimeMillis();
for(int i = 1; i < num; i++){
list.add(index, i);
}
return System.currentTimeMillis()-time;
}
}
复制代码
6. Run results
maxCount:10000
arrayData get time:1
linkedData get time:82
arrayData insert time:24
linkedData insert time:27
maxCount:30000
arrayData get time:2
linkedData get time:658
arrayData insert time:127
linkedData insert time:58
复制代码