Java LinkedList遍历的7种方法
LinkedList遍历方法
1. 一般的for循环(随机访问)遍历
int size = list.size(); for (int i=0; i<size; i++) { list.get(i); }
2. for--each循环遍历
for (Integer integ:list){ ; }
3. 迭代器iterator遍历
for(Iterator iter = list.iterator(); iter.hasNext();) iter.next();
4. 用pollFirst()遍历
while(list.pollFirst() != null) ;
5. 用pollLast()遍历
while(list.pollLast() != null) ;
6. 用removeFirst()遍历
try { while(list.removeFirst() != null) ; } catch (NoSuchElementException e) { }
7. 用removeLast()遍历
try { while(list.removeLast() != null) ; } catch (NoSuchElementException e) { }
实例
public class LinkedListTest { public static void main(String[] args) { LinkedList<Integer> llist = new LinkedList<Integer>(); for (int i=0; i<100000; i++) llist.addLast(i); byCommonFor(llist) ;// 通过一般for循环来遍历LinkedList byForEach(llist) ; // 通过for-each来遍历LinkedList byIterator(llist) ; // 通过Iterator来遍历LinkedList byPollFirst(llist) ; // 通过PollFirst()遍历LinkedList byPollLast(llist) ; // 通过PollLast()遍历LinkedList byRemoveFirst(llist) ; // 通过removeFirst()遍历LinkedList byRemoveLast(llist) ; // 通过removeLast()遍历LinkedList } private static void byCommonFor(LinkedList<Integer> list) {// 通过一般for循环来遍历LinkedList if (list == null) return ; long start = System.currentTimeMillis(); int size = list.size(); for (int i=0; i<size; i++) { list.get(i); } long end = System.currentTimeMillis(); long total = end - start; System.out.println("byCommonFor------->" + total+" ms"); } private static void byForEach(LinkedList<Integer> list) {// 通过for-each来遍历LinkedList if (list == null) return ; long start = System.currentTimeMillis(); for (Integer integ:list) ; long end = System.currentTimeMillis(); long total = end - start; System.out.println("byForEach------->" + total+" ms"); } private static void byIterator(LinkedList<Integer> list) {// 通过Iterator来遍历LinkedList if (list == null) return ; long start = System.currentTimeMillis(); for(Iterator iter = list.iterator(); iter.hasNext();) iter.next(); long end = System.currentTimeMillis(); long total = end - start; System.out.println("byIterator------->" + total+" ms"); } private static void byPollFirst(LinkedList<Integer> list) {//通过PollFirst()遍历LinkedList if (list == null) return ; long start = System.currentTimeMillis(); while(list.pollFirst() != null) ; long end = System.currentTimeMillis(); long total = end - start; System.out.println("byPollFirst------->" + total+" ms"); } private static void byPollLast(LinkedList<Integer> list) {// 通过PollLast()遍历LinkedList if (list == null) return ; long start = System.currentTimeMillis(); while(list.pollLast() != null) ; long end = System.currentTimeMillis(); long total = end - start; System.out.println("byPollLast------->" + total+" ms"); } private static void byRemoveFirst(LinkedList<Integer> list) {// 通过removeFirst()遍历LinkedList if (list == null) return ; long start = System.currentTimeMillis(); try { while(list.removeFirst() != null) ; } catch (NoSuchElementException e) { } long end = System.currentTimeMillis(); long total = end - start; System.out.println("byRemoveFirst------->" + total+" ms"); } private static void byRemoveLast(LinkedList<Integer> list) {// 通过removeLast()遍历LinkedList if (list == null) return ; long start = System.currentTimeMillis(); try { while(list.removeLast() != null) ; } catch (NoSuchElementException e) { } long end = System.currentTimeMillis(); long total = end - start; System.out.println("byRemoveLast------->" + total+" ms"); } }
运行结果:
byCommonFor------->5342 ms
byForEach------->11 ms
byIterator------->8 ms
byPollFirst------->4 ms
byPollLast------->0 ms
byRemoveFirst------->0 ms
byRemoveLast------->0 ms
由此可见,遍历LinkedList时,使用removeFist()或removeLast()效率最高。但用它们遍历时,会删除原始数据;若单纯只读取,而不删除,LinkedList遍历时建议使用For-each或者迭代器的方式。千万不要通过随机访问去遍历LinkedList!
版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。