對集合操作進行簡單的進行測試速度,數據量20w,對map,list,set,array,queue進行遍歷測試時間對比。
先粘貼一段對這些集合的介紹:
下面是測試代碼:
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