目录
- 一、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值组合集合。