目錄
- 一、ArrayList遍歷方式
- 1、普通for循環遍歷
- 2、增強for循環遍歷
- 3、Iterator迭代器遍歷
- 4、三種方式比較
- 二、Map遍歷方式
- 1、增強for循環 + keySet() 遍歷
- 2、增強for循環 + entrySet() 遍歷
- 3、Iterator + keySet() 遍歷
- 4、Itorator + entrySet() 遍歷
- 5、四種方式比較
- 三、java開發手冊(關于map的)
一、ArrayList遍歷方式
1、普通for循環遍歷
for(int i=0; i<lists.size(); i++){String key = lists.get(i);
}
2、增強for循環遍歷
for(String str : lists){String key = str;
}
3、Iterator迭代器遍歷
Iterator iterator = lists.iterator();
while (iterator.hasNext()){String key = iterator.next().toString();
}
4、三種方式比較
import java.util.ArrayList;
import java.util.Iterator;public class ArrayListFor {private static ArrayList<String> initData(){ArrayList<String> lists = new ArrayList<String>(1000000);for(int i=0; i<1000000; i++){lists.add(i + "abcdefg");}return lists;}private static String forOne(ArrayList<String> lists){StringBuilder sb = new StringBuilder();sb.append("普通For循環(int i=0; i<count; i++)。");sb.append("\r\n");long start = System.currentTimeMillis();for(int i=0; i<lists.size(); i++){String key = lists.get(i);}long end = System.currentTimeMillis();sb.append("duration = [" + (end - start) + "]ms");sb.append("\r\n");return sb.toString();}private static String forTwo(ArrayList<String> lists){StringBuilder sb = new StringBuilder();sb.append("加強For循環(int i : lists)。");sb.append("\r\n");long start = System.currentTimeMillis();for(String str : lists){String key = str;}long end = System.currentTimeMillis();sb.append("duration = [" + (end - start) + "]ms");sb.append("\r\n");return sb.toString();}private static String forThree(ArrayList<String> lists){StringBuilder sb = new StringBuilder();sb.append("Iterator循環(list.iterator())。");sb.append("\r\n");long start = System.currentTimeMillis();Iterator iterator = lists.iterator();while (iterator.hasNext()){String key = iterator.next().toString();}long end = System.currentTimeMillis();sb.append("duration = [" + (end - start) + "]ms");sb.append("\r\n");return sb.toString();}public static String forCycle(ArrayList<String> lists) {StringBuilder sb = new StringBuilder();sb.append("ArrayList遍歷比較:");sb.append("\r\n");sb.append(forOne(lists));sb.append(forTwo(lists));sb.append(forThree(lists));return sb.toString();}public static void main(String[] args) {ArrayList<String> lists = initData();for(int i=0; i<5; i++){System.out.println(forCycle(lists));}}
}
運行結果:
ArrayList遍歷比較:
普通For循環(int i=0; i<count; i++)。
duration = [33]ms
加強For循環(int i : lists)。
duration = [35]ms
Iterator循環(list.iterator())。
duration = [34]msArrayList遍歷比較:
普通For循環(int i=0; i<count; i++)。
duration = [29]ms
加強For循環(int i : lists)。
duration = [32]ms
Iterator循環(list.iterator())。
duration = [32]msArrayList遍歷比較:
普通For循環(int i=0; i<count; i++)。
duration = [26]ms
加強For循環(int i : lists)。
duration = [27]ms
Iterator循環(list.iterator())。
duration = [27]msArrayList遍歷比較:
普通For循環(int i=0; i<count; i++)。
duration = [26]ms
加強For循環(int i : lists)。
duration = [34]ms
Iterator循環(list.iterator())。
duration = [27]msArrayList遍歷比較:
普通For循環(int i=0; i<count; i++)。
duration = [26]ms
加強For循環(int i : lists)。
duration = [27]ms
Iterator循環(list.iterator())。
duration = [28]ms
總結:
普通for循環耗時最少,iterator次之,增強for循環耗時最長。
二、Map遍歷方式
大致分為 增強for循環
和 Iterator迭代器
兩種,而其中每個又各自分為使用 keySet()
和 entrySet()
兩種,所以 一共四種
。
1、增強for循環 + keySet() 遍歷
for(String key : maps.keySet()){String str = maps.get(key);
}
2、增強for循環 + entrySet() 遍歷
for(Map.Entry<String, String> entry : maps.entrySet()){String str = entry.getValue();
}
3、Iterator + keySet() 遍歷
Iterator iterator = maps.keySet().iterator();
while (iterator.hasNext()){String key = iterator.next().toString();String str = maps.get(key);
}
4、Itorator + entrySet() 遍歷
Iterator iterator = maps.entrySet().iterator();
while (iterator.hasNext()){Map.Entry<String, String> entry = (Map.Entry<String, String>)iterator.next();String str = entry.getValue();
}
5、四種方式比較
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;public class HashMapFor {private static HashMap<String,String> initData(){HashMap<String,String> maps = new HashMap<String, String>(10000000 * 2);for(int i=0; i<10000000; i++){String key = i + "abcdefg";maps.put(key, key);}return maps;}private static String forOne(HashMap<String, String> maps){StringBuilder sb = new StringBuilder();sb.append("增強For循環 + keySet()(int i : map.keySet())。");sb.append("\r\n");long start = System.currentTimeMillis();for(String key : maps.keySet()){String str = maps.get(key);}long end = System.currentTimeMillis();sb.append("duration = [" + (end - start) + "]ms");sb.append("\r\n");return sb.toString();}private static String forTwo(HashMap<String, String> maps){StringBuilder sb = new StringBuilder();sb.append("增強For循環 + entrySet()(Entry<> entry : map.entrySet())。");sb.append("\r\n");long start = System.currentTimeMillis();for(Map.Entry<String, String> entry : maps.entrySet()){String str = entry.getValue();}long end = System.currentTimeMillis();sb.append("duration = [" + (end - start) + "]ms");sb.append("\r\n");return sb.toString();}private static String forThree(HashMap<String, String> maps){StringBuilder sb = new StringBuilder();sb.append("Iterator循環 + keySet()(map.keySet().iterator())。");sb.append("\r\n");long start = System.currentTimeMillis();Iterator iterator = maps.keySet().iterator();while (iterator.hasNext()){String key = iterator.next().toString();String str = maps.get(key);}long end = System.currentTimeMillis();sb.append("duration = [" + (end - start) + "]ms");sb.append("\r\n");return sb.toString();}private static String forFour(HashMap<String, String> maps){StringBuilder sb = new StringBuilder();sb.append("Iterator循環 + entrySet()(map.entrySet().iterator())。");sb.append("\r\n");long start = System.currentTimeMillis();Iterator iterator = maps.entrySet().iterator();while (iterator.hasNext()){Map.Entry<String, String> entry = (Map.Entry<String, String>)iterator.next();String str = entry.getValue();}long end = System.currentTimeMillis();sb.append("duration = [" + (end - start) + "]ms");sb.append("\r\n");return sb.toString();}public static String forCycle(HashMap<String, String> maps) {StringBuilder sb = new StringBuilder();sb.append("HashMap遍歷比較:");sb.append("\r\n");sb.append(forOne(maps));sb.append(forTwo(maps));sb.append(forThree(maps));sb.append(forFour(maps));return sb.toString();}public static void main(String[] args) {HashMap<String, String> maps = initData();for(int i=0; i<5; i++){System.out.println(forCycle(maps));}}
}
運行結果:
HashMap遍歷比較:
增強For循環 + keySet()(int i : map.keySet())。
duration = [66]ms
增強For循環 + entrySet()(Entry<> entry : map.entrySet())。
duration = [46]ms
Iterator循環 + keySet()(map.keySet().iterator())。
duration = [68]ms
Iterator循環 + entrySet()(map.entrySet().iterator())。
duration = [45]msHashMap遍歷比較:
增強For循環 + keySet()(int i : map.keySet())。
duration = [63]ms
增強For循環 + entrySet()(Entry<> entry : map.entrySet())。
duration = [44]ms
Iterator循環 + keySet()(map.keySet().iterator())。
duration = [65]ms
Iterator循環 + entrySet()(map.entrySet().iterator())。
duration = [47]msHashMap遍歷比較:
增強For循環 + keySet()(int i : map.keySet())。
duration = [48]ms
增強For循環 + entrySet()(Entry<> entry : map.entrySet())。
duration = [37]ms
Iterator循環 + keySet()(map.keySet().iterator())。
duration = [72]ms
Iterator循環 + entrySet()(map.entrySet().iterator())。
duration = [34]msHashMap遍歷比較:
增強For循環 + keySet()(int i : map.keySet())。
duration = [54]ms
增強For循環 + entrySet()(Entry<> entry : map.entrySet())。
duration = [41]ms
Iterator循環 + keySet()(map.keySet().iterator())。
duration = [49]ms
Iterator循環 + entrySet()(map.entrySet().iterator())。
duration = [34]msHashMap遍歷比較:
增強For循環 + keySet()(int i : map.keySet())。
duration = [47]ms
增強For循環 + entrySet()(Entry<> entry : map.entrySet())。
duration = [31]ms
Iterator循環 + keySet()(map.keySet().iterator())。
duration = [47]ms
Iterator循環 + entrySet()(map.entrySet().iterator())。
duration = [31]ms
總結:
entrySet()
比keySet()
效率要好點Iterator
要比for each
效率要好點- 所以:
Iterator + entrySet()
效率最好(參考需謹慎)
三、java開發手冊(關于map的)
-
推薦使用
entrySet
遍歷Map
集合KV
,而不是使用keySet
方式遍歷 -
原因:
keySet
其實是遍歷了2次,一次是轉為Iterator
對象,另一次是從hashMap
中取出key
所對應的value
。而entrySet
只是遍歷了一次就把key
和value
都放到了entry
中,效率更高。如果是JDK8,使用Map.foreach
方法。 -
正例:
values()
返回的是V值集合,是一個list
集合對象,keySet()
返回的是 K值集合,是一個Set集合對象,entrySet()
返回的是 K-V值組合集合。