LinkedList的遍历

  1. 通过迭代器遍历。即通过Iterator去遍历。
for(Iterator iter = list.iterator(); iter.hasNext();)
    iter.next();
  1. 通过快速随机访问遍历LinkedList
int size = list.size();
for (int i=0; i<size; i++) {
    list.get(i);        
}
  1. 过另外一种for循环来遍历LinkedList
for (Integer integ:list) 
    ;

遍历实例及其效率

package day_3_29;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;

/*
 * @desc 测试LinkedList的几种遍历方式和效率
 *
 * @author Chenny
 */
public class LinkedListThruTest {
	public static void main(String[] args) {
		// 通过Iterator遍历LinkedList
//		iteratorLinkedListThruIterator(getLinkedList());
		//iteratorLinkedListThruIterator:304 ms

		
		// 通过快速随机访问遍历LinkedList
//		iteratorLinkedListThruForeach(getLinkedList());
//		iteratorLinkedListThruForeach:5057 ms

		
		// 通过for循环的变种来访问遍历LinkedList
//		iteratorThroughFor2(getLinkedList());
//		iteratorThroughFor2:296 ms

		
		// 通过PollFirst()遍历LinkedList
//		iteratorThroughPollFirst(getLinkedList());
//		iteratorThroughPollFirst:320 ms

		
		// 通过PollLast()遍历LinkedList
//		iteratorThroughPollLast(getLinkedList());
//		iteratorThroughPollLast:312 ms

		
		// 通过removeFirst()遍历LinkedList
//		iteratorThroughRemoveFirst(getLinkedList());
//		iteratorThroughRemoveFirst:312 ms

		
		// 通过removeLast()遍历LinkedList
//		iteratorThroughRemoveLast(getLinkedList());
//		iteratorThroughRemoveLast:322 ms
	}

	private static LinkedList getLinkedList() {
		LinkedList llist = new LinkedList();
//		ArrayList llist = new ArrayList();
		for (int i = 0; i < 100000; i++)
			llist.addLast(i);
//			llist.addFirst(i);
//			llist.add(i);
		return llist;
	}

	/**
	 * 通过快迭代器遍历LinkedList
	 */
	private static void iteratorLinkedListThruIterator(LinkedList<Integer> list) {
		if (list == null)
			return;

		// 记录开始时间
		long start = System.currentTimeMillis();

		for (Iterator iter = list.iterator(); iter.hasNext();)
			System.out.println(iter.next());

		// 记录结束时间
		long end = System.currentTimeMillis();
		long interval = end - start;
		System.out.println("iteratorLinkedListThruIterator:" + interval + " ms");
	}

	/**
	 * 通过快速随机访问遍历LinkedList
	 */
	private static void iteratorLinkedListThruForeach(LinkedList<Integer> list) {
		if (list == null)
			return;

		// 记录开始时间
		long start = System.currentTimeMillis();

		int size = list.size();
		for (int i = 0; i < size; i++) {

			System.out.println(list.get(i));
		}
		// 记录结束时间
		long end = System.currentTimeMillis();
		long interval = end - start;
		System.out.println("iteratorLinkedListThruForeach:" + interval + " ms");
	}

	/**
	 * 通过另外一种for循环来遍历LinkedList
	 */
	private static void iteratorThroughFor2(LinkedList<Integer> list) {
		if (list == null)
			return;

		// 记录开始时间
		long start = System.currentTimeMillis();

		for (Integer integ : list)
			System.out.println(integ);

		// 记录结束时间
		long end = System.currentTimeMillis();
		long interval = end - start;
		System.out.println("iteratorThroughFor2:" + interval + " ms");
	}

	/**
	 * 通过pollFirst()来遍历LinkedList
	 */
	private static void iteratorThroughPollFirst(LinkedList<Integer> list) {
		if (list == null)
			return;

		// 记录开始时间
		long start = System.currentTimeMillis();
		while(true) {
			Object obj = list.pollFirst();
			if(obj == null)
				break;
			System.out.println(obj);
		}

		// 记录结束时间
		long end = System.currentTimeMillis();
		long interval = end - start;
		System.out.println("iteratorThroughPollFirst:" + interval + " ms");
	}

	/**
	 * 通过pollLast()来遍历LinkedList
	 */
	private static void iteratorThroughPollLast(LinkedList<Integer> list) {
		if (list == null)
			return;

		// 记录开始时间
		long start = System.currentTimeMillis();
		while(true) {
			Object obj = list.pollLast();
			if(obj == null)
				break;
			System.out.println(obj);
		}

		// 记录结束时间
		long end = System.currentTimeMillis();
		long interval = end - start;
		System.out.println("iteratorThroughPollLast:" + interval + " ms");
	}

	/**
	 * 通过removeFirst()来遍历LinkedList
	 */
	private static void iteratorThroughRemoveFirst(LinkedList<Integer> list) {
		if (list == null)
			return;

		// 记录开始时间
		long start = System.currentTimeMillis();
		while(true) {		
			try {
				Object obj = list.removeFirst();
				System.out.println(obj);
			} catch (Exception e) {
				// TODO: handle exception
				break;
			}
		}

		// 记录结束时间
		long end = System.currentTimeMillis();
		long interval = end - start;
		System.out.println("iteratorThroughRemoveFirst:" + interval + " ms");
	}

	/**
	 * 通过removeLast()来遍历LinkedList
	 */
	private static void iteratorThroughRemoveLast(LinkedList<Integer> list) {
		if (list == null)
			return;

		// 记录开始时间
		long start = System.currentTimeMillis();
		while(true) {		
			try {
				Object obj = list.removeLast();
				System.out.println(obj);
			} catch (Exception e) {
				// TODO: handle exception
				break;
			}
		}

		// 记录结束时间
		long end = System.currentTimeMillis();
		long interval = end - start;
		System.out.println("iteratorThroughRemoveLast:" + interval + " ms");
	}

}

返回List

返回Java目录集合

你可能感兴趣的:(Java,LinkedList的遍历)