幾種集合遍歷速度對比


  對集合操作進行簡單的進行測試速度,數據量20w,對map,list,set,array,queue進行遍歷測試時間對比。

  先粘貼一段對這些集合的介紹:

1.1 Set接口  

  1. 存入Set的每個元素都必須是唯一的,Set接口不保證維護元素的次序;
  2. HashSet類: 為快速查找設計的Set,存入HashSet的對象必須定義hashCode(),它不保證集合的迭代順序;  
  3. LinkedHashSet類: 具有HashSet的查詢速度,且內部使用鏈表維護元素的順序(插入的次序)。

1.2 List接口

  1.  List按對象進入的順序保存對象,不做排序等操作;
  2.  ArrayList類:由數組實現的List,允許對元素進行快速隨機訪問,但是向List中間插入與移除元素的速度很慢;
  3.  LinkedList類: 對順序訪問進行了優化,向List中間插入與刪除的開銷並不大,隨機訪問則相對較慢。

1.3 Queue接口

  1. Queue用於模擬隊列這種數據結構,實現“FIFO”等數據結構。通常,隊列不允許隨機訪問隊列中的元素。
  2. ArrayDeque類:為Queue子接口Deque的實現類,數組方式實現。
  3. LinkedList類:是List接口的實現類,同時它也實現了Deque接口(Queue子接口)。因此它也可以當做一個雙端隊列來用,也可以當作“棧”來使用。

1.4 Map接口

  1.  添加、刪除操作put/remove/putAll/clear
  2.  查詢操作get/containsKey/containsValue/size/isEmpty
  3.  視圖操作keySet/values/entrySet
  4.  Map.Entry接口(Map的entrySet()方法返回一個實現Map.Entry接口的對象集合)  getKey/getValue/setValue

 

下面是測試代碼:

   

public static int leng = 200000;
	private String[] array;
	private Set<String> set;
	private List<String> list;
	private Queue<String> queue;
	private Map<String, String> map;

	@Before
	public void init() {
		array = new String[leng];
		set = new HashSet<String>();
		list = new ArrayList<String>();
		queue = new LinkedList<String>();
		map = new HashMap<String, String>();
		for (int i = 0; i < leng; i++) {
			String key = "didi:" + i;
			String value = "da";
			array[i] = key;
			set.add(key);
			list.add(key);
			queue.add(key);
			map.put(key, value);

		}
	}

	// shzu
	@Test
	public void testArray() {
		Long startTime = new Date().getTime();
		for (String sk : array) {
			///
		}
		Long endTime = new Date().getTime();
		Long times = endTime - startTime;
		System.out.println("時間:" + times);
	}

	// list
	@Test
	public void testList() {
		Long startTime = new Date().getTime();
		for (String sk : list) {
			///
		}
		Long endTime = new Date().getTime();
		Long times = endTime - startTime;
		System.out.println("時間:" + times);
	}

	// map
	@Test
	public void testMap() {
		Long startTime = new Date().getTime();
		for (Map.Entry<String, String> entry : map.entrySet()) {
			entry.getKey();
		}
		Long endTime = new Date().getTime();
		Long times = endTime - startTime;
		System.out.println("時間:" + times);
		Long startTime1 = new Date().getTime();
		for (String key : map.keySet()) {
			String value = (String) map.get(key);
		}
		Long endTime1 = new Date().getTime();
		Long times1 = endTime - startTime;
		System.out.println("時間1:" + times1);
	}
	

	// Queue
	@Test
	public void testQueue() {
		Long startTime = new Date().getTime();
		for (String s: queue) {
			//
		}
		Long endTime = new Date().getTime();
		Long times = endTime - startTime;
		System.out.println("時間1:" + times);
	}
	// Set
		@Test
		public void testSet() {
			Long startTime = new Date().getTime();
			for (String s: set) {
				//
			}
			Long endTime = new Date().getTime();
			Long times = endTime - startTime;
			System.out.println("時間:" + times);
		}

  時間:array:4ms,list:17ms,map:14ms,13ms,queue:15ms,set:14ms

     數據根據每次測試略有差距,但相差不大,array和其他差距最大,屬於 最快的遍歷,list最慢。

    參考資料:http://www.cnblogs.com/nayitian/archive/2013/03/08/2950730.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM