ArrayList和HashMap遍历比较
生活随笔
收集整理的這篇文章主要介紹了
ArrayList和HashMap遍历比较
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 一、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值組合集合。
總結
以上是生活随笔為你收集整理的ArrayList和HashMap遍历比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新手课堂之汽车灯光操作及位置
- 下一篇: 人寿保险是什么意思?