Java中 List、Set、Map遍历方式以及性能比较

目录

    一、简介
    二、遍历方式
        1、ArrayList遍历方式
            (1)for循环遍历
            (2)foreach循环遍历
            (3)Iterator迭代器遍历
        2、LinkedList遍历方式
            (1)for循环遍历
            (2)foreach循环遍历
            (3)Iterator迭代器遍历
        3、HashSet遍历方式
            (1)foreach循环遍历
            (2)Iterator迭代器遍历
        4、HashMap遍历方式
            (1)entrySet遍历
            (2)Iterator迭代器遍历
        5、LinkedHashMap遍历方式
            (1)entrySet遍历
            (2)Iterator迭代器遍历
    三、性能比较


一、简介

List、Set 都继承 Collection 接口,Map 不是。

    List:元素有序存储,元素可重复,取出来的顺序可能和放入的顺序不同,支持for循环和迭代器遍历;

    Set:元素无序存储,且唯一,不能包含重复的元素,不支持for循环遍历,支持迭代器遍历;

    Map:元素无序存储,key值唯一不能重复,value值可重复,支持迭代器遍历;

List、Set、Map实现类

    List:ArrayList、LinkedList、Vector

    Set:HashSet、TreeSet、LinkedHashSet

    Map:HashMap、TreeMap、HashTable、LinkedHashMap

线程安全 / 线程不安全

    线程安全:Vector、HashTable

    线程不安全:ArrayList、LinkedList、HashSet、LinkedHashSet、HashMap、TreeMap、LinkedHashMap

下面我们只拿出 ArrayList、LinkedList、HashSet、HashMap、LinkedHashMap 来讲解遍历方式以及遍历性能比较。



二、遍历方式
1、ArrayList遍历方式

ArrayList有三种遍历方式:for循环遍历、foreach循环遍历、Iterator迭代器遍历。

(1)for循环遍历
1
2
3
4
ArrayList<String> lists = new ArrayList<String>();
for(int i=0;i<lists.size();i++){
    String line = lists.get(i);
}

 

(2)foreach循环遍历

1
2
3
4
ArrayList<String> lists = new ArrayList<String>();
for(String str : lists){
    String line = str;
}

 

(3)Iterator迭代器遍历
1
2
3
4
5
ArrayList<String> lists = new ArrayList<String>();
Iterator<String> iterator = lists.iterator();
while (iterator.hasNext()){
    String line = iterator.next();
}

 

2、LinkedList遍历方式

LinkedList有三种遍历方式:for循环遍历、foreach循环遍历、Iterator迭代器遍历。

(1)for循环遍历

1
2
3
4
LinkedList<String> lists = new LinkedList<String>();
for(int i=0;i<lists.size();i++){
    String line = lists.get(i);
}

 

(2)foreach循环遍历

1
2
3
4
LinkedList<String> lists = new LinkedList<String>();
for(String str : lists){
    String line = str;
}

 

(3)Iterator迭代器遍历

1
2
3
4
5
LinkedList<String> lists = new LinkedList<String>();
Iterator<String> iterator = lists.iterator();
while (iterator.hasNext()){
    String line = iterator.next();
}

 

3、HashSet遍历方式

HashSet有两种遍历方式:foreach循环遍历、Iterator迭代器遍历。

(1)foreach循环遍历
1
2
3
4
HashSet<String> hashSets = new HashSet<String>();
for(String str : hashSets){
    String line = str;
}

 

(2)Iterator迭代器遍历

1
2
3
4
5
HashSet<String> hashSets = new HashSet<String>();
Iterator<String> iterator = hashSets.iterator();
while (iterator.hasNext()){
    String line = iterator.next();
}

 

4、HashMap遍历方式

HashMap有三种遍历方式:keySet循环遍历、entrySet遍历、Iterator迭代器遍历。

下面我们只讲解 entrySet遍历 和 Iterator迭代器遍历。

(1)entrySet遍历
1
2
3
4
HashMap<String, String> hashMaps = new HashMap<String, String>();
for(Map.Entry<String, String> entry : hashMaps.entrySet()){
    String line = entry.getKey();
}
(2)Iterator迭代器遍历

1
2
3
4
5
6
HashMap<String, String> hashMaps = new HashMap<String, String>();
Iterator iterator = hashMaps.entrySet().iterator();
while (iterator.hasNext()){
    Map.Entry<String, String> entry = (Map.Entry<String, String>)iterator.next();
    String line = entry.getKey();
}
5、LinkedHashMap遍历方式

LinkedHashMap有三种遍历方式:keySet循环遍历、entrySet遍历、Iterator迭代器遍历。

下面我们只讲解 entrySet遍历 和 Iterator迭代器遍历 。

(1)entrySet遍历

1
2
3
4
LinkedHashMap<String, String> linkedHashMaps = new LinkedHashMap<String, String>();
for(Map.Entry<String, String> entry : linkedHashMaps.entrySet()){
    String line = entry.getKey();
}

 

(2)Iterator迭代器遍历

1
2
3
4
5
6
LinkedHashMap<String, String> linkedHashMaps = new LinkedHashMap<String, String>();
Iterator iterator = linkedHashMaps.entrySet().iterator();
while (iterator.hasNext()){
    Map.Entry<String, String> entry = (Map.Entry<String, String>)iterator.next();
    String line = entry.getKey();
}

三、性能比较

不同数量级的性能差异是比较大的,下面我们分别在30、100、1000、10000、100000数量级进行性能比较。

完整代码如下:

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.example.springbootdemo.util;
 
import java.util.*;
 
public class Test {
 
    public static void main(String[] args) {
        compare();
    }
 
    private static ArrayList<String> lists = new ArrayList<String>();
    private static LinkedList<String> linkedLists = new LinkedList<String>();
 
    private static HashSet<String> hashSets = new HashSet<String>();
 
    private static HashMap<String, String> hashMaps = new HashMap<String, String>();
    private static LinkedHashMap<String, String> linkedHashMaps = new LinkedHashMap<String, String>();
 
    private static void compare(){
        compareInit(100000);
        compare1();
    }
 
    private static void compareInit(int count){
        lists.clear();
        linkedLists.clear();
        hashMaps.clear();
        hashSets.clear();
        linkedHashMaps.clear();
        String str = "abcdefg_";
        String one = "";
        for(int i=0;i<count;i++){
            one = str + i;
            lists.add(one);
            linkedLists.add(one);
            hashSets.add(one);
            hashMaps.put(one, one);
            linkedHashMaps.put(one, one);
        }
    }
 
    private static final String listFor               = "ArrayList for          duration";
    private static final String listForeach           = "ArrayList foreach      duration";
    private static final String listIterator          = "ArrayList Iterator     duration";
    private static final String linkedListFor         = "LinkedList for         duration";
    private static final String linkedListForeach     = "LinkedList foreach     duration";
    private static final String linkedListIterator    = "LinkedList Iterator    duration";
    private static final String hashSetForeach        = "HashSet foreach        duration";
    private static final String hashSetIterator       = "HashSet Iterator       duration";
    private static final String hashMapEntry          = "HashMap entry          duration";
    private static final String hashMapIterator       = "HashMap Iterator       duration";
    private static final String linkedHashMapEntry    = "LinkedHashMap entry    duration";
    private static final String linkedHashMapIterator = "LinkedHashMap Iterator duration";
 
    private static void compare1(){
        for(int i=0;i<5;i++){
            System.out.println("------------------------------");
            listOne();
            listTwo();
            listThree();
            linkedListOne();
            linkedListTwo();
            linkedListThree();
            hashSetOne();
            hashSetTwo();
            hashMapOne();
            hashMapTwo();
            linkedHashMapOne();
            linkedHashMapTwo();
            System.out.println();
        }
    }
 
    private static void listOne(){
        String line = "";
        long start = System.nanoTime();
        for(int i=0;i<lists.size();i++){
            line = lists.get(i);
        }
        long end = System.nanoTime();
        print(start, end, listFor);
    }
 
    private static void listTwo(){
        String line = "";
        long start = System.nanoTime();
        for(String str : lists){
            line = str;
        }
        long end = System.nanoTime();
        print(start, end, listForeach);
    }
 
    private static void listThree(){
        String line = "";
        long start = System.nanoTime();
        Iterator<String> iterator = lists.iterator();
        while (iterator.hasNext()){
            line = iterator.next();
        }
        long end = System.nanoTime();
        print(start, end, listIterator);
    }
 
    private static void linkedListOne(){
        String line = "";
        long start = System.nanoTime();
        for(int i=0;i<linkedLists.size();i++){
            line = linkedLists.get(i);
        }
        long end = System.nanoTime();
        print(start, end, linkedListFor);
    }
 
    private static void linkedListTwo(){
        String line = "";
        long start = System.nanoTime();
        for(String str : linkedLists){
            line = str;
        }
        long end = System.nanoTime();
        print(start, end, linkedListForeach);
    }
 
    private static void linkedListThree(){
        String line = "";
        long start = System.nanoTime();
        Iterator<String> iterator = linkedLists.iterator();
        while (iterator.hasNext()){
            line = iterator.next();
        }
        long end = System.nanoTime();
        print(start, end, linkedListIterator);
    }
 
    private static void hashSetOne(){
        String line = "";
        long start = System.nanoTime();
        for(String str : hashSets){
            line = str;
        }
        long end = System.nanoTime();
        print(start, end, hashSetForeach);
    }
 
    private static void hashSetTwo(){
        String line = "";
        long start = System.nanoTime();
        Iterator<String> iterator = hashSets.iterator();
        while (iterator.hasNext()){
            line = iterator.next();
        }
        long end = System.nanoTime();
        print(start, end, hashSetIterator);
    }
 
    private static void hashMapOne(){
        String line = "";
        long start = System.nanoTime();
        for(Map.Entry<String, String> entry : hashMaps.entrySet()){
            line = entry.getKey();
        }
        long end = System.nanoTime();
        print(start, end, hashMapEntry);
    }
 
    private static void hashMapTwo(){
        String line = "";
        long start = System.nanoTime();
        Iterator iterator = hashMaps.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> entry = (Map.Entry<String, String>)iterator.next();
            line = entry.getKey();
        }
        long end = System.nanoTime();
        print(start, end, hashMapIterator);
    }
 
    private static void linkedHashMapOne(){
        String line = "";
        long start = System.nanoTime();
        for(Map.Entry<String, String> entry : linkedHashMaps.entrySet()){
            line = entry.getKey();
        }
        long end = System.nanoTime();
        print(start, end, linkedHashMapEntry);
    }
 
    private static void linkedHashMapTwo(){
        String line = "";
        long start = System.nanoTime();
        Iterator iterator = linkedHashMaps.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> entry = (Map.Entry<String, String>)iterator.next();
            line = entry.getKey();
        }
        long end = System.nanoTime();
        print(start, end, linkedHashMapIterator);
    }
 
    private static void print(long start, long end, String tip){
        System.out.println(tip + " = [" + ((double)((end - start)/1000))/1000 + "]ms");
    }
}


我们经过多轮测试,取相对合理的结果进行展示,单位为:毫秒(ms)

单个类型不同遍历方式性能比较总结:

    ArrayList:三种遍历方式性能差距不大,数量级较小时,for循环遍历更优,数量级较大时,Iterator迭代器遍历方式性能更优;

    LinkedList:三种遍历方式中for循环遍历性能最差,其他两种方式性能差距比较小,但是Iterator迭代器遍历方式性能更优;

    HashSet:两种遍历方式性能差距不大,但是Iterator迭代器遍历方式性能更优;

    HashMap:两种遍历方式性能差距不大,但是Iterator迭代器遍历方式性能更优;

    LinkedHashMap:两种遍历方式性能差距不大,但是Iterator迭代器遍历方式性能更优;


整体性能比较总结:

    同等数量级,ArrayList的遍历性能更优;

posted @   锐洋智能  阅读(1075)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
历史上的今天:
2022-01-05 Pandas和talib对比
2022-01-05 java判断日期大小,大于等于,小于等于
2022-01-05 mysql as if 多个,mysql as多个if语句
2010-01-05 使用JS+WebBrowser解决Web打印问题(完成事件)
点击右上角即可分享
微信分享提示