We often use arraylist traversal to summarize the differences between for and get () traversal and iterator with next () traversal. We will go to the Java JDK source code for in-depth analysis.
For more information, see the test procedure at http://bbs.csdn.net/topics/250025827:
Import Java. util. iterator; import Java. util. list; import Java. util. arraylist; import Java. util. shortlist;/*** iteratortest ** @ author sagezk */public class iteratortest {public static long testforloops (list <string> List) {long start = 0l; long end = 0l; start = system. nanotime (); For (INT I = List. size ()-1; I> = 0; -- I) {list. get (I);} End = system. nanotime (); return end-start;} public static long testiterator (list <string> List) {long start = 0l, end = 0l; Start = system. nanotime (); iterator <string> it = List. iterator (); While (it. hasnext () {It. next ();} End = system. nanotime (); return end-start;} public static void main (string [] ARGs) {// test list length final int Len = 10000; // list <string> arraylist = new arraylist <string> (); List <string> struct list = new struct list <string> (); for (INT I = 0; I <Len; ++ I) {string S = integer. tostring (I, 2); arraylist. add (s); values list. add (s) ;}// print the test result final string format = "% 1 $-16 S % 2 $-16 S % 3 $ 16D \ n"; system. out. println ("list \ t \ tType \ t \ tTime (nanoseconds)"); system. out. println ("-------------------------------------------------"); system. out. printf (format, "arraylist", "for", testforloops (arraylist); system. out. printf (format, "arraylist", "iterator", testiterator (arraylist); system. out. printf (format, "shortlist", "for", testforloops (shortlist); system. out. printf (format, "shortlist", "iterator", testiterator (shortlist ));}}
Run the program:
Listtypetime (nanoseconds)
-------------------------------------------------
Arraylist for 607064
Arraylist iterator 809922
Counter list for 154326455
Using list iterator 958140
We can see that the gap is obvious if you use the sorted list linked list to traverse the list.
If you use the arraylist, use for faster.
Based on the difference here, I tracked the source code.
Arraylist iterator and for-Get traverse next
public E next() { checkForComodification(); int i = cursor; if (i >= size)throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length)throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i];}
Get
private transient Object[] elementData;public E get(int index) {rangeCheck(index);return elementData(index);}E elementData(int index) {return (E) elementData[index];}
We can see that the two methods use the return (e) elementdata [Index] method to read the node from the object [] array, so the difference is as follows, the read Speed of get for traversal is a little faster but not obvious, because next () also needs to perform a value assignment operation and basic type assignment.
Iterator and for-get of the sorted list traverse next
public E next() { checkForComodification(); if (!hasNext())throw new NoSuchElementException(); lastReturned = next; next = next.next; nextIndex++; return lastReturned.item;}private Node<E> next;private static class Node<E> {E item;Node<E> next;Node<E> prev;Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev;}}
Get
public E get(int index) {checkElementIndex(index);return node(index).item;}
It can be seen that the next in the chain list is directly read by the chain node one by one. The next reading speed of the chain node here can be a relatively optimistic and obvious distance from the get reading speed of the for loop.
To sum up the short comment, use next () for chained lists. If only the general arraylist is used for traversal, use the for and get combinations. With the combination of for and get, the code is simple in line with the habits of General programmers, and the efficiency is high, so the traversal of arraylist in the future should not be entangled.
This article from the csdn blog, reproduced please contact the author to indicate the source http://blog.csdn.net/dreamintheworld
Source code analysis the differences between iterator interface traversal and direct for-Get combination Traversal