總的來講, 有5種遍歷, 其中1種是僅遍歷了values, 4種徹底遍歷了整個的map, 分類和原理介紹以下html
Map的遍歷:
遍歷方式: 分爲foreach和iterator兩種, foreach效率好一點. 這是方式上面的區別, 還有在使用的遍歷因子的不一樣;
遍歷因子: 按照遍歷因子來劃分, 則有keySet()和entrySet()兩種, map的內部功能有Map.Entry接口實現, keySet()是直接取到keys, entrySet是首先取到每個entry, 而後使用entry的getKey() 和 getValue()來完整的實現遍歷, 速度要更好.java
import lombok.extern.slf4j.Slf4j; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * @Author: renjiaxin * @Despcription: * @Date: Created in 2018/8/17 9:52 * @Modified by: */ @Slf4j public class CreateTraversal { public static Map<String, Object> createMap() { Map<String, Object> map = new HashMap<>(); map.put("天海翼", "36C"); map.put("小澤瑪利亞", "42F"); map.put("", "");//能夠的, 這樣定義也能夠 map.put(null, null);/*key能夠爲null, 可是隻能有一個, 只有一個起做用*/ map.put(null, null); map.put("波多野結衣", "36C"); map.put("蒼井空", "28B"); map.put(" ", " ");//能夠的, 這樣定義也能夠 map.put("小倉優子", "26A"); map.put("深田恭子", null); map.put("雨宮琴音", "36C"); map.put("北條麻妃", null);//value爲null, 資料暫空, value爲null能夠有多個 map.put("青山葵", "43F"); return map; } public static void traversalMap(Map<String, Object> map, int type) { if (type < 0 || type > 4) { log.warn("錯誤的遍歷類型, type:{}", type); } switch (type) { case 0: partForEachTraversal(map); break; case 1: forEachKeySetTraversal(map); break; case 2: forEachEntrySetTraversal(map); break; case 3: iteratorKeySetTraversal(map); break; case 4: System.out.println("推薦map.entrySet()+iterator"); iteratorEntrySetTraversal(map); break; default: iteratorEntrySetTraversal(map); } } /*只是遍歷了values, 沒有遍歷keys*/ private static void partForEachTraversal(Map<String, Object> map) { System.out.println("\n使用values來循環, 只能遍歷values, 沒法遍歷keys"); long start = System.currentTimeMillis(); for (Object value : map.values()) { System.out.print(value + " "); } long end = System.currentTimeMillis(); long time = start - end; System.out.println("\nmap的大小n: " + map.values().size()); System.out.println("運行時間爲: " + time + "ms"); System.out.println(""); } /*map.keySet()+foreach*/ private static void forEachKeySetTraversal(Map<String, Object> map) { System.out.println("map.keySet()+foreach, 完成遍歷, 速度慢!"); long start = System.currentTimeMillis(); for (String key : map.keySet()) { System.out.print("key: " + key + ", values: " + map.get(key) + "; "); } long end = System.currentTimeMillis(); long time = start - end; System.out.println("\nmap的大小n: " + map.values().size()); System.out.println("運行時間爲: " + time + "ms"); System.out.println(""); } /*map.entrySet()+foreach*/ private static void forEachEntrySetTraversal(Map<String, Object> map) { System.out.println("map.entrySet()+foreach, 完成遍歷, 速度快!"); System.out.println("Map.Entry是Map類內部的一個接口,提供了Map類的主體方法和功能"); long start = System.currentTimeMillis(); Set<Map.Entry<String, Object>> entrySet = map.entrySet();//把這個set取出來 for (Map.Entry<String, Object> entry : entrySet) {/*---*/ System.out.print("key= " + entry.getKey() + " and value= " + entry.getValue() + "; "); } long end = System.currentTimeMillis(); long time = start - end; System.out.println("\nmap的大小n: " + map.values().size()); System.out.println("運行時間爲: " + time + "ms"); System.out.println(""); } /*map.keySet()+iterator*/ private static void iteratorKeySetTraversal(Map<String, Object> map) { System.out.println("map.keySet()+iterator, 完成遍歷"); long start = System.currentTimeMillis(); Iterator<String> it = map.keySet().iterator();/**/ while (it.hasNext()) { System.out.print("key :" + it.next() + " , value: " + map.get(it.next())); } long end = System.currentTimeMillis(); long time = start - end; System.out.println("\nmap的大小n: " + map.values().size()); System.out.println("運行時間爲: " + time + "ms"); System.out.println(""); } /*map.entrySet()+iterator*/ private static void iteratorEntrySetTraversal(Map<String, Object> map) { System.out.println("map.entrySet()+iterator, 完成遍歷, 推薦的作法!"); long start = System.currentTimeMillis(); //Iterator it = map.entrySet().iterator();/*---*/ Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();//上下相同, 這個清晰一些 while (it.hasNext()) { System.out.print("key :" + it.next() + " , value: " + map.get(it.next())); } long end = System.currentTimeMillis(); long time = start - end; System.out.println("\nmap的大小n: " + map.values().size()); System.out.println("運行時間爲: " + time + "ms"); System.out.println(""); } public static void main(String[] args) { System.out.println("因爲map使用key-value形式的數據結構,因此沒有使用數字形式的位置,不可以使用for(int i=0;i<n;i++形式)"); Map<String, Object> mp = createMap(); traversalMap(mp, 0); traversalMap(mp, 1); traversalMap(mp, 2); traversalMap(mp, 3); traversalMap(mp, 4); } }
ref:android
1.遍歷HashMap的四種方法, 2.HashMap遍歷和使用, 3.Java中如何遍歷Map對象的4種方法, 4.Java中遍歷Map的各類方式, 5.HashMap遍歷的兩種方式,推薦使用entrySet(), 6.HashMap循環遍歷方式及其性能對比, 7.JCFInternalsgit