List collection is self-evident importance, directly open to dry. ( The following tests are based on openjdk1.8 )
ArrayList: the bottom is used in an array, so slow additions and deletions, look fast.
Slow additions: for example, you now have an array inside the 5 elements (corresponding to the subscript 0-4), you now need to add elements to mark down 2 places, then the array subscript elements 2,3,4 on need to move it backward, but if you just added to the end of the array, it does not move (except lead to a set of expansion); deleted too, will lead to delete the middle element behind moved forward.
Fast Find: can be positioned by the array element directly to the subscript (the time complexity is O (1)).
Traversal: for traversing the loop iterator traversal and are time complexity O (n), but there are differences, as follows:
for (int i = 0; i < list.size(); i+=3) {//n 表示你要找几个元素
list.get(i);
}
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {//n 表示list的长度
iterator.next();
}
LinkedList: the bottom layer is used in the linked list, so fast deletions
Fast deletions: deletions only need to move the pointer to a linked list of nodes , to find slow
look slow: Start from the list next to a head of a ratio (time complexity of O (n)).
Traverse: for looping through, time complexity is O ( n ^ 2); iterator traversal time complexity of O (n), as follows:
for (int i = 0; i < list.size(); i+=3) {
list.get(i);//每一次get,都需要遍历一次链表
}
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {//只会遍历一次链表
iterator.next();//每调用一次next,只是将遍历的指针往后面移一次
}
Find ArrayList fast additions and deletions slow, LinkedList look slow additions and deletions fast.
Here we ponder this question: to insert 10000 collection element, and then traverse it, the faster List? If the number increased to 100,000, the List is faster?
Directly in code prove:
public class TestList {
public static void main(String[] args) {
testArrayList(10000);
System.out.println("=============================");
testLinkedList(10000);
}
public static void testArrayList(int num) {
long start = System.nanoTime();
List<Integer> list = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
for (int i = 0; i < num; i++) {
list.add(i);
}
long addEndTime = System.nanoTime();
System.out.println("ArrayList insert spend time:" + (addEndTime - start) / 100);
for (int i = 0; i < num; i++) {
//每次都从中间添加新元素,这样后半部分的元素就需要往后移动
//add(int index, Integer element)
list2.add(i/2, i);
}
long addEndToTime = System.nanoTime();
System.out.println("ArrayList insertTo spend time:" + (addEndToTime - addEndTime) / 100);
for (int i = 0; i < list.size(); i++) {
list.get(i);
}
long forEndTime = System.nanoTime();
System.out.println("ArrayList foreach spend time:" + (forEndTime - addEndTime) / 100);
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
iterator.next();
}
long end = System.nanoTime();
System.out.println("ArrayList iterator spend time:" + (end - forEndTime) / 100);
}
public static void testLinkedList(int num) {
long start = System.nanoTime();
List<Integer> list = new LinkedList<Integer>();
for (int i = 0; i < num; i++) {
list.add(i);
}
long addEndTime = System.nanoTime();
System.out.println("LinkedList insert spend time:" + (addEndTime - start) / 100);
for (int i = 0; i < list.size(); i++) {
list.get(i);
}
long forEndTime = System.nanoTime();
System.out.println("LinkedList foreach spend time:" + (forEndTime - addEndTime) / 100);
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
iterator.next();
}
long end = System.nanoTime();
System.out.println("LinkedList iterator spend time:" + (end - forEndTime) / 100);
}
}
The above code tests the ArrayList and LinkedList inserted to 10,000 elements, and then loop through and for the time it takes to traverse iterator respectively. Look console print:
ArrayList insert spend time:14534
ArrayList insertTo spend time:45437 ##新元素都从集合中间增加
ArrayList foreach spend time:55484
ArrayList iterator spend time:12512 ##遍历所有,iterator还是比foreach快
=============================
LinkedList insert spend time:15932
LinkedList foreach spend time:360552 ##linkedList用for循环遍历效率是真低呀
LinkedList iterator spend time:13550
Look at the amount of data is below 100,000 when it takes time
ArrayList insert spend time:64547
ArrayList insertTo spend time:3093679
ArrayList foreach spend time:3127564
ArrayList iterator spend time:23360
=============================
LinkedList insert spend time:67861
LinkedList foreach spend time:33846313
LinkedList iterator spend time:24305
to sum up:
- ArrayList always inserted at the end if set, and the insertion efficiency is almost LinkedList; if ArrayLsit random insertion position, the insertion efficiency is worse than LinkedList, when the data size becomes larger, the difference will be more obvious.
- ArrayList and LinkedList with **** iterator traversal cycle faster than for traversal ** (data amount is small, such as tens, or iterator for Arraylist with little difference), when the amount of data, the gap more clearly;
- When a large amount of data, it is best not to loop through LinkedList use for .
Posted below ArrayList -> add (int index, Integer element) Method Source:
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1); // Increments modCount!!
//元素移动
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
//然后赋值
elementData[index] = element;
size++;
}