其他分享
首页 > 其他分享> > Map遍历

Map遍历

作者:互联网

一:for循环方式

entrySet的for循环

/**
     * entrySet的for循环方式
     * @param map
     */
    private static void entrySetForMethod(Map<String, String> map){
        long startTime = System.currentTimeMillis();
        for (Map.Entry<String, String> entry:map.entrySet()) {
            String key =entry.getKey();
            String value =entry.getValue();
//            System.out.println("entrySet的for循环方式:"+entry.getKey()+"--->"+entry.getValue());
        }
        long endTime = System.currentTimeMillis();
        System.out.println("entrySetForMethod时间" + (endTime - startTime));
    }
keySet的for循环
/**
     * keySet的for循环方式
     * @param map
     */
    private static void keySetForMethod(Map<String, String> map){
        long startTime = System.currentTimeMillis();
        for (String key : map.keySet()) {
            String value =map.get(key);
//            System.out.println("keySet的for循环方式:"+key+"--->"+map.get(key));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("keySetForMethod时间" + (endTime - startTime));
    }

二:iterator迭代器

entrySet的iterator迭代
/**
     * entrySet的iterator迭代器
     * @param map
     */
    private static void entrySetIteratorMethod(Map<String, String> map){
        long startTime = System.currentTimeMillis();
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            String key =iterator.next().getKey();
            String value =iterator.next().getValue();
//            System.out.println("entrySet的iterator迭代器:"+iterator.next().getKey()+"--->"+iterator.next().getValue());
        }
        long endTime = System.currentTimeMillis();
        System.out.println("entrySetIteratorMethod时间" + (endTime - startTime));
    }
keySet的iterator迭代
/**
     * keySet的iterator迭代器
     * @param map
     */
    private static void keySetIteratorMethod(Map<String, String> map){
        long startTime = System.currentTimeMillis();
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key =iterator.next();
            String value =map.get(iterator.next());
//            System.out.println("keySet的iterator迭代器:"+iterator.next()+"--->"+map.get(iterator.next()));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("keySetIteratorMethod时间" + (endTime - startTime));
    }

三:jdk8的foreach循环

/**
     * jdk8Foreach循环方式
     * @param map
     */
    private static void jdk8ForeachMethod(Map<String, String> map){
        long startTime = System.currentTimeMillis();
        map.forEach((key,value)->{
            String value2 =value;
            String key2 =key;
//            System.out.println("jdk8Foreach循环方式:"+key+"--->"+value);
        });
        long endTime = System.currentTimeMillis();
        System.out.println("jdk8Foreach时间" + (endTime - startTime));
    }

测试这几种方式的效率

public static void main(String[] args) {
        Map<String,String> map=new HashMap<>();
        for (int i = 0; i < 1000000; i++) {
            map.put(i + "", Integer.toString(new Random().nextInt(100000)));
        }
        entrySetForMethod(map);
        entrySetIteratorMethod(map);
        keySetForMethod(map);
        keySetIteratorMethod(map);
        jdk8ForeachMethod(map);
    }

执行结果

 

 

标签:Map,遍历,String,iterator,map,System,startTime,out
来源: https://blog.csdn.net/qq_31811587/article/details/122126133