几种集合遍历速度对比

  对集合操作进行简单的进行测试速度,数据量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

原文地址:https://www.cnblogs.com/jeyson/p/5371058.html