对集合操作进行简单的进行测试速度,数据量20w,对map,list,set,array,queue进行遍历测试时间对比。
先粘贴一段对这些集合的介绍:
1.1 Set接口
- 存入Set的每个元素都必须是唯一的,Set接口不保证维护元素的次序;
- HashSet类: 为快速查找设计的Set,存入HashSet的对象必须定义hashCode(),它不保证集合的迭代顺序;
- LinkedHashSet类: 具有HashSet的查询速度,且内部使用链表维护元素的顺序(插入的次序)。
1.2 List接口
- List按对象进入的顺序保存对象,不做排序等操作;
- ArrayList类:由数组实现的List,允许对元素进行快速随机访问,但是向List中间插入与移除元素的速度很慢;
- LinkedList类: 对顺序访问进行了优化,向List中间插入与删除的开销并不大,随机访问则相对较慢。
1.3 Queue接口
- Queue用于模拟队列这种数据结构,实现“FIFO”等数据结构。通常,队列不允许随机访问队列中的元素。
- ArrayDeque类:为Queue子接口Deque的实现类,数组方式实现。
- LinkedList类:是List接口的实现类,同时它也实现了Deque接口(Queue子接口)。因此它也可以当做一个双端队列来用,也可以当作“栈”来使用。
1.4 Map接口
- 添加、删除操作put/remove/putAll/clear
- 查询操作get/containsKey/containsValue/size/isEmpty
- 视图操作keySet/values/entrySet
- Map.Entry接口(Map的entrySet()方法返回一个实现Map.Entry接口的对象集合) getKey/getValue/setValue
下面是测试代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
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最慢。